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

ChatGPT Free Online Download the ChatGPT App Easily

Have you ever wanted to talk to a friendly, smart robot that helps you with…

7 days ago

Free GPT Chat? DeepSeek AI Does It Better

DeepSeek AI is becoming a friendly and powerful new tool in the world of artificial…

2 weeks ago

Spring Boot MySQL Complete CRUD REST API [ Free Sourecode ]

Do you want to become an expert in Spring Boot CRUD operations? This comprehensive tutorial…

3 weeks ago

CREATE Responsive Navigation Bar with FlexBox CSS!

Modern websites must have a navigation bar that is clear and responsive. FlexBox CSS is…

4 weeks ago

Registration with image upload Java Jdbc(Download Source code)

Introduction In this section, we will guide you step by step in the development of an image upload registration system in Java using MySQL and JDBC. In the application, users register…

2 months ago

Touchable shop Pos system using Java

The Touchable Shop POS (Point of Sale) system is a sophisticated software solution developed using…

3 months ago