Java

Java Project Step by Step Using Postgresql Database

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.

Feature of projects

  • The system shall be able to record student details : name,address,phone.
  • The system  shall be able to retrieve the student details : name,address,phone.
  • Then system shall be able to Edit and Delete the student details : name,address,phone.

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 Postgresql.

Step 3: Download Postgresql. Click here   follow the steps and install it.

 

Establish the database connection

    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);
        }
    }

 

AddRecords

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);
        }


View Records

       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.

Form Constructor


   public StudentCrud() {
        initComponents();
        Connect();
        Table();
    

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();
        
        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());


Edit

you can use the following code snippet to Edit the records.

 

Paste code inside the Edit button.


        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);
        }



Delete

you can use the following code snippet to Delete the records.

Paste code inside the Delete button.

  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.

 

admin

Recent Posts

Employee Working Hours Calculation System using C#.net

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

1 week ago

Java Payroll System Calculate Employee Overtime

Act as a Java developer to create a program that calculates the gross wage for…

2 weeks ago

Employee Working Hours Calculation System using Java

Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…

2 weeks ago

Laravel 11 School Management System

In this tutorial, we will teach you how to create a simple school management system…

2 weeks ago

How to Make Admin Panel Using React MUI

I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…

4 weeks ago

Install Laravel Breeze for Authentication Using Laravel 11

In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…

1 month ago