This java JDBC crud will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE. using Mysql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.
The tutorial you learn java mysql crud operation step by step. Crud operations are must when you developing a java mini projects. learn how learn how to INSERT, SELECT, UPDATE and DELETE in database by writing code to manage the records table in the database named linda. records table consist of following columns name, course, fee.
we will do the java best practice for understand java programming clearly and easily.
Feature of projects
- The system shall be able to record student details : name,course,fee.
- The system shall be able to retrieve the student details : name,course,fee.
- Then system shall be able to Edit and Delete the student details : name,course,fee .
Learn how to make this System Step by step
Step 1: Download JDK Click here follow the steps and install it.
Step 2: Download an appropriate jdbc driver Click here in order to connect jdbc and mysql.
Step 3: Download xampp server Click here follow the steps and install it.
after installed xampp server in your computer. you shall be able to create datebase. I attached screen shot below to create the database.
Create the Database name linda
After created the database have to create the table which name is “records” the table consist of following Columns name,mobile,course.
AddRecords
you can use the following code snippet to add the records in to database. paste the code inside the add button
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | String name =txtname.getText(); String age =txtmobile.getText(); String course =txtcourse.getText(); try { Class.forName("com.mysql.jdbc.Driver"); //Register the mysql driver con1 = DriverManager.getConnection("jdbc:mysql://localhost/linda","root",""); insert = con1.prepareStatement("insert into record (name,mobile,course)values(?,?,?)"); insert.setString(1,name); insert.setString(2,age); insert.setString(3,course); insert.executeUpdate(); JOptionPane.showMessageDialog(this, "Record Saved"); txtname.setText(""); txtmobile.setText(""); txtcourse.setText(""); table_update(); } catch (ClassNotFoundException ex) { Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(crud.class.getName()).log(Level.SEVERE, null, ex); } |
View Records
you can use the following code snippet to retrieve the data stored in the database and present it to users in a proper format. create a method table_update() .paste the method inside the constructor of the class.when the form is loaded all the records will be shown on the jTable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | private void table_update() { int CC; try { Class.forName("com.mysql.jdbc.Driver"); con1 = DriverManager.getConnection("jdbc:mysql://localhost/linda","root",""); insert = con1.prepareStatement("SELECT * FROM record"); ResultSet Rs = insert.executeQuery(); ResultSetMetaData RSMD = Rs.getMetaData(); CC = RSMD.getColumnCount(); DefaultTableModel DFT = (DefaultTableModel) jTable1.getModel(); DFT.setRowCount(0); while (Rs.next()) { Vector v2 = new Vector(); for (int ii = 1; ii <= CC; ii++) { v2.add(Rs.getString("id")); v2.add(Rs.getString("name")); v2.add(Rs.getString("mobile")); v2.add(Rs.getString("course")); } DFT.addRow(v2); } } catch (Exception e) { } } |
Form Constructor
| public crud() { initComponents(); table_update(); // when the form is loaded all the records will be shown on the jTable. } |
Edit return
you can use the following code snippet to select row of the jTable.after selected the row records will displyed in a relavent jTextfields.
Paste code inside the jTableMouseClicked event
| DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); int selectedIndex = jTable1.getSelectedRow(); txtname.setText(model.getValueAt(selectedIndex, 1).toString()); txtmobile.setText(model.getValueAt(selectedIndex, 2).toString()); txtcourse.setText(model.getValueAt(selectedIndex, 3).toString()); |
Edit
you can use the following code snippet to Edit the records.
Paste code inside the Edit button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); int selectedIndex = jTable1.getSelectedRow(); try { int id = Integer.parseInt(model.getValueAt(selectedIndex, 0).toString()); String name =txtname.getText(); String mobile =txtmobile.getText(); String course =txtcourse.getText(); Class.forName("com.mysql.jdbc.Driver"); con1 = DriverManager.getConnection("jdbc:mysql://localhost/linda","root",""); insert = con1.prepareStatement("update record set name= ?,mobile= ?,course= ? where id= ?"); insert.setString(1,name); insert.setString(2,mobile); insert.setString(3,course); insert.setInt(4,id); insert.executeUpdate(); JOptionPane.showMessageDialog(this, "Record Updated"); txtname.setText(""); txtmobile.setText(""); txtcourse.setText(""); table_update(); } catch (ClassNotFoundException ex) { } catch (SQLException ex) { } |
Delete
you can use the following code snippet to Delete the records.
Paste code inside the Delete button.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); int selectedIndex = jTable1.getSelectedRow(); try { int id = Integer.parseInt(model.getValueAt(selectedIndex, 0).toString()); int dialogResult = JOptionPane.showConfirmDialog (null, "Do you want to Delete the record","Warning",JOptionPane.YES_NO_OPTION); if(dialogResult == JOptionPane.YES_OPTION){ Class.forName("com.mysql.jdbc.Driver"); con1 = DriverManager.getConnection("jdbc:mysql://localhost/linda","root",""); insert = con1.prepareStatement("delete from record where id = ?"); insert.setInt(1,id); insert.executeUpdate(); JOptionPane.showMessageDialog(this, "Record Delete"); txtname.setText(""); txtmobile.setText(""); txtcourse.setText(""); table_update(); } } catch (ClassNotFoundException ex) { } catch (SQLException ex) { } |
0 Comments