Java

Java crud step by step in Eclipse

This java JDBC will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE in Eclipse. 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 in database by writing code to manage the book table in the database named  javacrudbook  table consist of following columns name,edition,price.

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

Feature of projects

The system shall be able to record Book details : name,edition,price. The system  shall be able to retrieve the Book details : name,edition,price. Then system shall be able to Edit and Delete the Book details : name,edition,price.

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. Establish the database connection

Connection con;
PreparedStatement pst;
ResultSet rs;

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

    }

 

AddRecords

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

public void actionPerformed(ActionEvent e) 
{   
    String bname,edition,price;
    bname = txtbname.getText();
    edition = txtedition.getText();
    price = txtprice.getText();
                
     try {
        pst = con.prepareStatement("insert into book(name,edition,price)values(?,?,?)");
        pst.setString(1, bname);
        pst.setString(2, edition);
        pst.setString(3, price);
        pst.executeUpdate();
        JOptionPane.showMessageDialog(null, "Record Addedddd!!!!!");
        table_load();
                       
        txtbname.setText("");
        txtedition.setText("");
        txtprice.setText("");
        txtbname.requestFocus();
       }

    catch (SQLException e1) 
        {   
       e1.printStackTrace();
    }
}

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_load() .paste the method inside the constructor of the class.when the form is loaded all the records will be shown on the jTable.

public void table_load()
{
 try 
 {
    pst = con.prepareStatement("select * from book");
    rs = pst.executeQuery();
    table.setModel(DbUtils.resultSetToTableModel(rs));
} 
 catch (SQLException e) 
  {
  e.printStackTrace();
  } 
}

Form Constructor

public JavaCrud() {
    initialize();
    Connect();
    table_load();
}

Search Records

Enter the book id on textfield relavent book information will be displayed on the relavent textfield. textfield event select as keyReleased event

public void keyReleased(KeyEvent e) {
                
                 try {
                      
                        String id = txtbid.getText();

                            pst = con.prepareStatement("select name,edition,price from book where id = ?");
                            pst.setString(1, id);
                            ResultSet rs = pst.executeQuery();

                        if(rs.next()==true)
                        {
                          
                            String name = rs.getString(1);
                            String edition = rs.getString(2);
                            String price = rs.getString(3);
                            
                            txtbname.setText(name);
                            txtedition.setText(edition);
                            txtprice.setText(price);
    
                        }   
                        else
                        {
                         txtbname.setText("");
                         txtedition.setText("");
                            txtprice.setText("");
                             
                        }
                    } 
                
                 catch (SQLException ex) {
                       
                    }
            }

Edit

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

Paste code inside the Edit button.

                
                
                String bname,edition,price,bid;
                
                
                bname = txtbname.getText();
                edition = txtedition.getText();
                price = txtprice.getText();
                bid  = txtbid.getText();
                
                 try {
                        pst = con.prepareStatement("update book set name= ?,edition=?,price=? where id =?");
                        pst.setString(1, bname);
                        pst.setString(2, edition);
                        pst.setString(3, price);
                        pst.setString(4, bid);
                        pst.executeUpdate();
                        JOptionPane.showMessageDialog(null, "Record Update!!!!!");
                        table_load();
                       
                        txtbname.setText("");
                        txtedition.setText("");
                        txtprice.setText("");
                        txtbname.requestFocus();
                    }

                    catch (SQLException e1) {
                        
                        e1.printStackTrace();
                    }
    
           

Delete

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

Paste code inside the Delete button.

           String bid;
           bid  = txtbid.getText();
           
            try {
                   pst = con.prepareStatement("delete from book where id =?");
           
                   pst.setString(1, bid);
                   pst.executeUpdate();
                   JOptionPane.showMessageDialog(null, "Record Delete!!!!!");
                   table_load();
                  
                   txtbname.setText("");
                   txtedition.setText("");
                   txtprice.setText("");
                   txtbname.requestFocus();
               }

               catch (SQLException e1) {
                   
                   e1.printStackTrace();
               }

 

i have attached the video link below. which will do this tutorials step by step.

admin

Recent Posts

Creating Grocery Inventory App Using React

Introduction to Grocery Inventory Apps Managing grocery inventory can be a daunting task, but with…

31 minutes ago

Fish Inventory Shop Management System in Angular

This article explain how to make a Fish Inventory Management App in Angular.this app explain…

6 days ago

Fish Inventory Management with React

Introduction to Fish Inventory Management In the aquaculture industry, managing fish inventory is crucial for…

1 week ago

Java GUI CRUD for Beginners

Introduction to Java GUI CRUD Java is a powerful programming language widely used for building…

1 week ago

Creating Beautiful Login Form Design Using React

Introduction to Login Form Design Designing an effective and beautiful login form is crucial for…

2 weeks ago

Creating Responsive Login Form with React

Introduction In today creating a responsive login form is essential for providing a seamless user…

2 weeks ago