Sunday, 22 September 2019

Connect JAVA with MYSQL database | insert Data into Table Example Source Code

/* action perform event for insert data into table
 Database : student_info
Table : class5
root username of mysql db
mysql@123 is a password of db

*/
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                       
        // TODO add your handling code here:
        //first get rollno and name enter by user
        String rollNo = txtRollNo.getText();
        String Name = txtName.getText();
       
        //convert roll no into integer
        int RNo = Integer.parseInt(rollNo);
        //connection
        try{
            //open connection
            Class.forName("com.mysql.jdbc.Driver");
           
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/student_info","root","mysql@123");
            //here student_info is a database
            //root is username of mysql and mysql@123 is a password
           
            //for insert data into table
            Statement stm = con.createStatement();
           
            String sql = "INSERT INTO CLASS5  VALUES("+RNo+",'"+Name+"')";
            //class5 in above queary is a table
            //EXECUTE STATEMENT
            stm.executeUpdate(sql); //here insert query apply
           
            //display message of record inserted
            JOptionPane.showMessageDialog(this, "student Record Inserted");
            txtName.setText("");
            txtRollNo.setText("");
            //close connection
            con.close();
           
           
        }catch(Exception e){
            System.out.println(e.getMessage());
        }
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
       
    }                                        

3 comments:

  1. Replies
    1. https://dev.mysql.com/downloads/connector/j/

      Delete
  2. Can you please share code for the jbutton that executes the program

    ReplyDelete

JTable Hide and Show in Java Swing