This java JDBC crud will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE. using Postgresql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.this lesson will help you to learn java connect Postgresql Database.
The tutorial you learn java Postgresql crud operation step by step. Crud operations are must when you developing a java mini projects. learn how to INSERT, SELECT, UPDATE and DELETE in database by writing code to manage the records table in the database named lindaschool. records table consist of following columns name,address,phone.
we will do the java best practice for understand java programming clearly and easily.
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 Postgresql.
Step 3: Download Postgresql. Click here follow the steps and install it.
Connection con; PreparedStatement pst; public void Connect() { try { Class.forName("org.postgresql.Driver"); con = DriverManager.getConnection("jdbc:postgresql://localhost:5434/Lindaschool", "postgres", "123" ); } catch (ClassNotFoundException ex) { Logger.getLogger(StudentCrud.class.getName()).log(Level.SEVERE, null, ex); } catch (SQLException ex) { Logger.getLogger(StudentCrud.class.getName()).log(Level.SEVERE, null, ex); } }
you can use the following code snippet to add the records in to database. paste the code inside the add button
String name,address; int phone; name = txtName.getText(); address = txtAddress.getText(); phone = Integer.parseInt(txtPhone.getText()); try { pst = con.prepareStatement("insert into records (name,address,phone)values(?,?,?)"); pst.setString(1,name); pst.setString(2,address); pst.setInt(3,phone); pst.executeUpdate(); Table(); txtName.setText(""); txtAddress.setText(""); txtPhone.setText(""); txtName.requestFocus(); JOptionPane.showMessageDialog(this, "Successfully Saved"); } catch (SQLException ex) { Logger.getLogger(StudentCrud.class.getName()).log(Level.SEVERE, null, ex); }
public void Table() { try { pst = con.prepareStatement("SELECT * FROM records"); ResultSet Rs = pst.executeQuery(); DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); ResultSetMetaData RSMD = Rs.getMetaData(); int 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.getInt("id")); v2.add(Rs.getString("name")); v2.add(Rs.getString("address")); v2.add(Rs.getInt("phone")); } DFT.addRow(v2); } jTable1.setModel(model); } catch (SQLException ex) { ex.printStackTrace(); } }
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.
public StudentCrud() { initComponents(); Connect(); Table();
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(); int id = Integer.parseInt(model.getValueAt(selectedIndex, 0).toString()); txtName.setText(model.getValueAt(selectedIndex, 1).toString()); txtAddress.setText(model.getValueAt(selectedIndex, 2).toString()); txtPhone.setText(model.getValueAt(selectedIndex, 3).toString());
you can use the following code snippet to Edit the records.
String name,address; int phone; DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); int selectedIndex = jTable1.getSelectedRow(); int id = Integer.parseInt(model.getValueAt(selectedIndex, 0).toString()); name = txtName.getText(); address = txtAddress.getText(); phone = Integer.parseInt(txtPhone.getText()); try { pst = con.prepareStatement("update records set name =?,address = ?,phone =? where id = ?"); pst.setString(1,name); pst.setString(2,address); pst.setInt(3,phone); pst.setInt(4,id); pst.executeUpdate(); Table(); txtName.setText(""); txtAddress.setText(""); txtPhone.setText(""); txtName.requestFocus(); JOptionPane.showMessageDialog(this, "Successfully Updatedd"); } catch (SQLException ex) { Logger.getLogger(StudentCrud.class.getName()).log(Level.SEVERE, null, ex); }
you can use the following code snippet to Delete the records.
String name,address; int phone; DefaultTableModel model = (DefaultTableModel) jTable1.getModel(); int selectedIndex = jTable1.getSelectedRow(); int id = Integer.parseInt(model.getValueAt(selectedIndex, 0).toString()); try { pst = con.prepareStatement("delete from records where id = ?"); pst.setInt(1,id); pst.executeUpdate(); Table(); txtName.setText(""); txtAddress.setText(""); txtPhone.setText(""); txtName.requestFocus(); JOptionPane.showMessageDialog(this, "Successfully Deleteddd"); } catch (SQLException ex) { Logger.getLogger(StudentCrud.class.getName()).log(Level.SEVERE, null, ex); }
i have attached the video link below. which will do this tutorials step by step.
In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…
In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel 11 CRUD…
in this tutorials we will be talk about how to make a times table in…
In this tutorials will teach How to add two numbers in Laravel 10. (more…)
In this tutorial, we will teach the process of building a full-stack application using Node.js,…
In this tutorial, we will explin you through the process of building a Hospital Management…