Java

Java Mysql simple project in IntelliJ IDEA

This Java Myql crud application will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE in IntelliJ IDEA using Mysql Database. IntelliJ IDEA is a very famous editor to writing the Java codes. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.

We will learn how to INSERT, SELECT, UPDATE and DELETE in database by writing code to manage the products table in the database named gbproducts.  products  table consist of following columns Product name,price,qty.

Feature of projects

The system shall be able to record Product details : product name,price,qty.

The system shall be able to retrieve the Product details : product name, price,qty.

Then system shall be able to Edit and Delete the product details : product name, price,qty..

Learn how to make this System Step by step

Jdbc connection in java

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;

 public void Connect()
    {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection("jdbc:mysql://localhost/gbproducts", "root","");
            System.out.println("Success");
        }
        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

String name,price,qty;

name = txtName.getText();
price = txtPrice.getText();
qty = txtQty.getText();

try {
      pst = con.prepareStatement("insert into products(pname,price,qty)values(?,?,?)");
      pst.setString(1, name);
      pst.setString(2, price);
      pst.setString(3, qty);
      pst.executeUpdate();
      JOptionPane.showMessageDialog(null,"Record Addedddddd!!!!");

      txtName.setText("");
      txtPrice.setText("");
      txtQty.setText("");
      txtName.requestFocus();
   }

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

Search Records

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

try {

      String pid = txtpid.getText();
      pst = con.prepareStatement("select pname,price,qty from products where pid = ?");
      pst.setString(1, pid);
       ResultSet rs = pst.executeQuery();

       if(rs.next()==true)
           {
               String name = rs.getString(1);
               String price = rs.getString(2);
               String qty = rs.getString(3);

               txtName.setText(name);
               txtPrice.setText(price);
               txtQty.setText(qty);
           }
          else
           {
                txtName.setText("");
                txtPrice.setText("");
                txtQty.setText("");
                JOptionPane.showMessageDialog(null,"Invalid Product ID");

             }
   }

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

Edit

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

Paste code inside the Edit button.

 String pid,name,price,qty;

 name = txtName.getText();
 price = txtPrice.getText();
 qty = txtQty.getText();
 pid = txtpid.getText();

   try {
         
         pst = con.prepareStatement("update products set pname = ?,price = ?,qty = ? where pid = ?");
         pst.setString(1, name);
         pst.setString(2, price);
         pst.setString(3, qty);
         pst.setString(4, pid);

         pst.executeUpdate();
         JOptionPane.showMessageDialog(null, "Record Updateee!!!!!");

         txtName.setText("");
         txtPrice.setText("");
         txtQty.setText("");
         txtName.requestFocus();
         txtpid.setText("");
      }

    catch (SQLException e1)
      {

         e1.printStackTrace();
       }

Delete

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

Paste code inside the Delete button.

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