Tuesday, December 18, 2012

                      Database Updating


Today im going to show you how to update a  recode in a database in MySql Lets Go on
Create this kind of form




 i want to update my databace after clicking update button
heres the coding for this form


















  • Now you have to create a java class named DBConnection because we have created a object in our form
  • In
    DBConnection class write following coding as in the photo
 
  • now we are redy to add data foor our boos tabel in datavace called my
  • Guys i briefly explained this application If you ave any Questions plese add a comment im ready to answer them Cu guys from another tutorial

Monday, December 17, 2012

package dbconnect;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;
/**
 *
 * @author Akila
 */
public class data extends javax.swing.JFrame {
Connection con;
Statement stmt;
ResultSet rs;
    /**
     * Creates new form data
     */
    public data()  {
        initComponents();
       DoConnect();
      
       setLocation(290, 275);
      
      
      
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                         
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        t1 = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        t2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        t3 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        t4 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jButton6 = new javax.swing.JButton();
        jButton7 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Book Title");

        t1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t1ActionPerformed(evt);
            }
        });

        jLabel2.setText("Book ID");

        jLabel3.setText("Author");

        t3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t3ActionPerformed(evt);
            }
        });

        jLabel4.setText("Publisher");

        t4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                t4ActionPerformed(evt);
            }
        });

        jButton1.setText("First");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Last");

        jButton3.setText("Next");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Previous");

        jButton5.setText("Update");

        jButton6.setText("Delete Record");

        jButton7.setText("New Record");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton7, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(96, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(t1)
                            .addComponent(t2)
                            .addComponent(t3)
                            .addComponent(t4, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addGap(39, 39, 39)
                        .addComponent(jButton2)
                        .addGap(42, 42, 42)
                        .addComponent(jButton3)
                        .addGap(40, 40, 40)
                        .addComponent(jButton4)))
                .addGap(101, 101, 101))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(t1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(t2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(t3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(42, 42, 42)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(t4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(57, 57, 57)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton5)
                    .addComponent(jButton6)
                    .addComponent(jButton7))
                .addGap(31, 31, 31))
        );

        pack();
    }// </editor-fold>                       

    private void t1ActionPerformed(java.awt.event.ActionEvent evt) {                                  
        // TODO add your handling code here:
    }                                 

    private void t3ActionPerformed(java.awt.event.ActionEvent evt) {                                  
        // TODO add your handling code here:
    }                                 

    private void t4ActionPerformed(java.awt.event.ActionEvent evt) {                                  
        // TODO add your handling code here:
    }                                 

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:
    }                                       

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:'
        try{
            if(rs.next()){
                String id  = rs.getString("BookId");
            
             String booktitle = rs.getString("Title");
             String pub = rs.getString("publisher");
             String auth = rs.getString("author");
             
            
            
            t1.setText(booktitle);
            t2.setText(id);
            t3.setText(auth);   
            t4.setText(pub);
               
            }
            /*
            else{
               
                rs.previous();
                JOptionPane.showMessageDialog(data.this, "End Of the records");
            }
            */
        }
       
       
        catch(SQLException err){
           
            JOptionPane.showMessageDialog(data.this, err.getMessage());
           
           
           
        }
       
       
    }                                       

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(data.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
               
                    new data().setVisible(true);
               
            }
        });
    }
    // Variables declaration - do not modify                    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JButton jButton6;
    private javax.swing.JButton jButton7;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField t1;
    private javax.swing.JTextField t2;
    private javax.swing.JTextField t3;
    private javax.swing.JTextField t4;
    // End of variables declaration                  

    public void DoConnect()  {
       try {
           Class.forName("com.mysql.jdbc.Driver");   
           try {
       
              
               String host = "jdbc:mysql://localhost:3306/my";
              
               String uName = "root";
               String uPass= "";
               
               Connection con = DriverManager.getConnection(host,uName,uPass);
                 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE );
                
                
                 String SQL = "Select * from books";
                
                 ResultSet rs = stmt.executeQuery(SQL);
                   
                rs.next(); 
                
                 String id  = rs.getString("BookId");
                
                
                 String booktitle = rs.getString("Title");
                 String pub = rs.getString("publisher");
                 String auth = rs.getString("author");
                 
                
                
                t1.setText(booktitle);
                t2.setText(id);
                t3.setText(auth);   
                t4.setText(pub);
               
                
             }
            catch (SQLException ex) {
      
               
               JOptionPane.showMessageDialog(this,ex.getMessage());
          
            
            
             }
        }
        catch (ClassNotFoundException ex) {
  
           
            Logger.getLogger(data.class.getName()).log(Level.SEVERE, null,ex);
      
        
        
         }
    }
}
Hey guys after week i had a problem again whwn i execute thos in netbens following errors occor
this is the source

in my next button i want to go next recode heres da nxt button code

 private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                        
        // TODO add your handling code here:'
        try{
            if(rs.next()){
                String id  = rs.getString("BookId");
            
             String booktitle = rs.getString("Title");
             String pub = rs.getString("publisher");
             String auth = rs.getString("author");
             
            
            
            t1.setText(booktitle);
            t2.setText(id);
            t3.setText(auth);   
            t4.setText(pub);
               
            }
            else{
               
                rs.previous();
                JOptionPane.showMessageDialog(data.this, "End Of the records");
            }
           
           
        }
       
        catch(SQLException err){
           
            JOptionPane.showMessageDialog(data.this, err.getMessage());
           
           
           
        }
       
       
    }          



the error is this

 Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at dbconnect.data.jButton3ActionPerformed(data.java:205)
    at dbconnect.data.access$400(data.java:19)
    at dbconnect.data$5.actionPerformed(data.java:101)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6504)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6269)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4860)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2713)
    at java.awt.Component.dispatchEvent(Component.java:4686)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
    at java.awt.EventQueue.access$000(EventQueue.java:101)
    at java.awt.EventQueue$3.run(EventQueue.java:666)
    at java.awt.EventQueue$3.run(EventQueue.java:664)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:680)
    at java.awt.EventQueue$4.run(EventQueue.java:678)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
BUILD SUCCESSFUL (total time: 4 seconds)

        
Lets find solution guys!!!!!!!!!!!!!!!!!!!

Wednesday, December 12, 2012

New Thing




hell guys This tutorial im going to show you how to make a print out from your printer in java like this

 

 

 first you have to create class file PrintDemo.java in your platform  then you can use it as a you needed

lets go on

 

 

 import java.awt.Graphics;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;

/**
 *
 * @author Admin
 */
public class PrintDemo implements Printable{

    public PrintDemo() {
        PrinterJob p = PrinterJob.getPrinterJob();
        p.setPrintable(this);
        if(p.printDialog()){
            try{
                p.print();
            }catch(Exception e){}
        }
    }

    public static void main(String[] arg){
        PrintDemo pd = new PrintDemo();

    }

    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

}


Enjoy it and leave a comment see you  Guys from a next tutorial Bye

 

Monday, December 10, 2012

hello guys today i'm going to show you a problem which i have been faced..................

I had a problem in netbeans That is passing valus from one form to another form.....Lets do it now

i want  to get user name from this form and pass it to another form let go on

In the login button type this chord


        String user = jTextField1.getText();
        String pw = new String(jPasswordField1.getPassword());
       
        if(user.equals("akila")&& pw.equals("akila")){
           
            this.setVisible(false);
            new voteing(user).setVisible(true);
           
        }
else {
           
            JOptionPane.showMessageDialog(null, "Incorrect user name or password","Error",3);
        }
      
now we are passing String user to the another form called voteing ,,,You can also pass double int or anything like this ....In net beans now you will get a  error.......now netbeans will ask you to creaTE A CONSTRUCTOR in next form ........Let netbeans to do that




this is my next form you can see at the bottem user akila has benn passed to this form..Lets see how to create a constructor in this form .....Go to sourcr and type in the bottem in jFrame


 voteing(String user) {
       
        username = user;
       
       
        initComponents();
    }



Now, right click your welcome form and go to>Events>Window>windowOpened and use the username variable set it wherever you want!
Example:-

label.setText(username);
Now, go back to login form and press Shift + F6 to run your file. Enter your username and password and click login to see your name on the next screen.

I THINK YOU HAVE GAINED SOMETHING IN THIS TUTORIAL..CU GUYS FROM THE NEXT TUTORIAL.................................................................








Sunday, December 2, 2012

ohh VERY GOOD NEW I FOUND HOW TO MADE SAVE BUUTTON ON NOTEPAD !!!
HERE THE WAY

 // TODO add your handling code here:
        JFileChooser save = new JFileChooser();
       
        int option = save.showSaveDialog(this);
       
        if(option == JFileChooser.APPROVE_OPTION){
           
            try {
                BufferedWriter out = new BufferedWriter(new FileWriter(save.getSelectedFile().getPath()));
               
                out.write(this.jTextArea1.getText());
                out.close();
               
            }
            catch(Exception ex){
               
                System.out.println(ex.getMessage());
               
               
            }
           
        }



THANKS EVERYONE FOR HELPING ME








Saturday, December 1, 2012

  These days i'm creating a  notepad in Netbeans but i Got a problem my Save button does not work can anyone help me on this this is my chording





  private void jMenuItem3ActionPerformed(java.awt.event.ActionEvent evt) {                                          
        // TODO add your handling code here:
        FileFilter ft = new FileNameExtensionFilter("Text Files","txt");
         FileFilter ft1 = new FileNameExtensionFilter("HTML Files","html");
        db.addChoosableFileFilter(ft);
        db.addChoosableFileFilter(ft1);
        int returnVal = db.showSaveDialog(this);
       
        if(returnVal == javax.swing.JFileChooser.APPROVE_OPTION){
           
            java.io.File save_file = db.getSelectedFile();
            String File_name =  save_file.toString();
           
            try{
               
                write data = new write(File_name, true);
                String alltext = jTextArea1.getText();
                data.writeToFile(alltext);
               
            }
            catch(IOException e ){
               
               
            }
           
           
          
        }
       

       
       
       

Thursday, November 29, 2012

 Examples Content

 

1 . Java fundamentals

2.  Java Operators

3.  Loops

4   Arrays

5   Exception Handling

6   Java IO

7   Java Characters and Strings