Let’s learn how to insert data or values into a database like MySQLusing jFrame form in Appache Netbeans IDE.
jFrame From
Create a jFrame form after creating a new project or create with your existing project in Appache Netbeans as below image.
Create jFrame From
Design your jFrame from with labels and textfield and buttons. in this case we are proceeding a student registrations form as below image.
Code
Data or values must be inserted into MySQL Database while clicking the ‘SUBMIT’ button. Let’s start writing some code to do that into the ‘jButtonActionPerfromed’ function by double clicking the ‘Submit’ button. Before that Please make sure your connection to MySQL Database server has been established. Connect Netbeans project to MysQL DataBase. private void jButtonSubmitActionPerformed(java.awt.event.ActionEvent evt) {
try {
// TODO add your handling code here:
con1 = DBConnect.connect();
Statement stmt = con1.createStatement();
if(con1==null)
JOptionPane.showMessageDialog(this,"Unable to Connect DB");
String query = "INSERT INTO `student`(`student_id`, `student_name`, `course`) VALUES ('"+jTextFieldStduentID.getText()+"','"+jTextFieldStudentName.getText()+"','"+jTextFieldCourse.getText()+"')";
stmt.executeUpdate(query);
JOptionPane.showMessageDialog(this, "Submitted");
} catch (SQLException ex) {
Logger.getLogger(Student.class.getName()).log(Level.SEVERE, null, ex);
}
}
Run jFrame and See the Output.