Inserting textbox string in a table-MYSQL using NetBeans

In this java tutorial i am going to share with you how to insert textbox string or values in table of MySQL database using netbeans Java.

Java-Netbeans Insert Data into Database
1.1 Java-Netbeans Inserting Data into Database


private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
         
         String rno=Rollno.getText();
         String nam=Name.getText();
        try {

 //class.forname("com.mysql.jdbc.Driver");
        
  cn=DriverManager.getConnection("jdbc:mysql://localhost:3306/student", "Your Username", "Your Password");
  st=cn.createStatement();
    
}catch(Exception ex){
    System.out.println("Error : "+ex);
 
}
         try {
            
            String query="select * from student_Master";
            st.executeUpdate("insert into student_Master (Rollno,Name) values('"+rno+"','"+nam+"')");
            rs=st.executeQuery(query);
            //String query1="insert into student_Master(rollno,name)" + "values('2','NNNN') ";
            //rs=st.executeUpdate(query1);
            
            System.out.println("Records from Database...!");
            while(rs.next()) {
                String Rollno=rs.getString("rollno");
                String Name=rs.getString("name");
                System.out.println("Rollno "+Rollno+ "  " + "name  "+Name);
                
                }
         }catch(Exception ex){
            System.out.println(ex);
        }
    }    
Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon