Home Java Java JDBC CRUD tutorial Insert,Update,Delete,Search

Java JDBC CRUD tutorial Insert,Update,Delete,Search

9 min read
0
1
7,211

This Java JDBC CRUD tutorial will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE and SEARCH 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 to INSERT, SELECT, UPDATE and DELETE and  SEARCH in database by writing code to manage the products table in the database named smobileproducts table consist of following columns product name,price ,qty.

we will do the java best practice for understand java programming clearly and easily.

Learn how to make this System Step by step

  • The system shall be able to record product details : product name,price ,qty.
  • The system  shall be able to able retrieve the product details : product name,price ,qty.
  • Then system shall be able to  Edit and Delete the product details : product name,price ,qty .
  • Then system shall be able  to Search the product details : product name,price ,qty .

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.

you have to install xampp server in your computer Click here.after that you shall be able to create datebase.

To connect the database in Java  please refer the following code. To create the method name connect (); paste the below code inside the Connect () method.

 Connection con;         //create the connection object con
 PreparedStatement pst; // create the PreparedStatement object pst
 ResultSet rs;          // create the ResultSet object rs
 Statement stmt;        // create the Statement object stmt

    public void Connect()
    {
        try 
        {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/smobile", "root", "");  
        } 
        catch (ClassNotFoundException ex)
        {
            ex.printStackTrace();
        } 
        catch (SQLException ex) 
        {
            ex.printStackTrace();
        }
    }

Add Records

you can use the following code snippet to add the records in to database. paste the code inside the add button

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
      
        String pname = txtpname.getText();
        String price = txtprice.getText();
        String qty = txtqty.getText();

        try 
        {
            pst = con.prepareStatement("insert into products(pname,price,qty)values(?,?,?)");
            pst.setString(1, pname);
            pst.setString(2, price);
            pst.setString(3, qty);
            int k = pst.executeUpdate();
            if(k==1)
            {
                JOptionPane.showMessageDialog(this, "Record Adddeddd");
                txtpname.setText("");
                txtprice.setText("");
                txtqty.setText("");
                txtpname.requestFocus();
                LoadProductno();
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Record Failed");
            }
            
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(products.class.getName()).log(Level.SEVERE, null, ex);
        }
    }                                        

Search Records

Load the Product ID from the Combo box 

you can use the following code snippet to load the product id from the database. you have to create the function LoadProductno().paste the code inside the LoadProductno() as i shown the below of code.

 public void LoadProductno() 
            { 
                try 
                { 
                pst = con.prepareStatement("SELECT id FROM products"); 
                rs = pst.executeQuery(); 
                txtpid.removeAllItems();

                while (rs.next()) 
                  { 
                     txtpid.addItem(rs.getString(1)); 
                  } 
                } 
                catch ( Exception e) 
                { 
                   e.printStackTrace(); 
                } 
            }

Select the relavent product id from the combobox and click on search button data will be displayed on the relavent textfields. attached the screen shot image above.

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

private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) 
{                                         
        String pid =  txtpid.getSelectedItem().toString();
             try 
                { 
                pst = con.prepareStatement( "SELECT * FROM products where id = ?" ); 
                pst.setString(1, pid);
                rs = pst.executeQuery(); 
    
             if ( rs.next() == true ) 
                { 
                 txtpname.setText(rs.getString(2)); 
                 txtprice.setText(rs.getString(3)); 
                 txtqty.setText(rs.getString(4)); 
                } 
             else
             {
                 JOptionPane.showMessageDialog(this, "Record Not Found");
             }
                } 
                catch ( Exception e ) 
                { 
                e.printStackTrace(); 
                }  
    }

Update Records

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

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         

        String pname = txtpname.getText();
        String price = txtprice.getText();
        String qty = txtqty.getText();
        String pid =  txtpid.getSelectedItem().toString();
        try 
        {
            pst = con.prepareStatement("update products set pname = ?, price = ?, qty= ? where id = ?");
            pst.setString(1, pname);
            pst.setString(2, price);
            pst.setString(3, qty);
             pst.setString(4, pid);
            int k = pst.executeUpdate();
            if(k==1)
            {
                JOptionPane.showMessageDialog(this, "Record Updatedddddddd");
                txtpname.setText("");
                txtprice.setText("");
                txtqty.setText("");
                txtpname.requestFocus();
                LoadProductno();
            }
            else
            {
                JOptionPane.showMessageDialog(this, "Record Failed");
            }
            
        }
        catch (SQLException ex) 
        {
            Logger.getLogger(products.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
    }

Delete Records

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

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
 
              try {
 
            String pid = txtpid.getSelectedItem().toString();
             
            pst = con.prepareStatement("delete from products where id =?");
            pst.setString(1, pid);
            int k = pst.executeUpdate();
            
            if(k==1)
            {
                JOptionPane.showMessageDialog(this, "Record Deleteedddddd");
                txtpname.setText("");
                txtprice.setText("");
                txtqty.setText("");
                txtpname.requestFocus();
                 LoadProductNo();
            }
            else
            {
                 JOptionPane.showMessageDialog(this, "Record Failed");
            }
            
            
            
        } catch (SQLException ex) {
            Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex);
        }

        
    }

I have attached the video tutorial below it will help you  to do this  step by step.

Load More Related Articles
Load More By admin
Load More In Java

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Inventory Management with Form Repeater in Laravel | Step-by-Step Tutorial

in this tutorils will explain how to make a Form Repeater in Laravel Step by step. …