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

Do I Return in Void Function in Java?

In Java, a void function (or method) does not return any value. However, you can use the return statement…

2 hours ago

Is Java Hard to Learn?

Is Java Hard to Learn is it True? Java is one of the most popular…

3 hours ago

How to generate random with weight java

Generating Random Numbers with Weights in Java Random number generation with weighted probabilities is a…

3 hours ago

Building JSP AJAX CRUD Application

Introduction to JSP AJAX CRUD Applications Building web applications has become more dynamic with the…

2 days ago

Hotel Management System using Laravel 11

Relationships: Hotel ↔ Rooms (One-to-Many) A hotel can have many rooms, but a room belongs…

3 weeks ago

Creating Grocery Inventory App Using React

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

1 month ago