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

Laravel 11 CRUD Mastering RESTful API MVC with Repository Pattern

In this tutorial will teach Laravel 11 Api MVC with Repository Pattern Crud Application step…

1 day ago

Laravel 11 CRUD Application

In this tutorial will teach Laravel 11 CRUD Application step by step. Laravel  11 CRUD…

4 weeks ago

How to make Times Table in React

in this tutorials we will be talk about how to make a times table in…

1 month ago

Laravel Tutorial: How to Add Numbers Easily Laravel 10

In this tutorials will teach How to add two numbers in Laravel 10. (more…)

1 month ago

Build Full-Stack Node.js MongoDB CRUD App with JWT Authentication

In this tutorial, we will teach the process of building a full-stack application using Node.js,…

2 months ago

Hospital Management System using OOP Java and MySQL

In this tutorial, we will explin you through the process of building a Hospital Management…

3 months ago