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

Conditional Statements in Python

Conditional statements in Python allow us to control the flow of execution based on conditions.…

3 weeks ago

Java Beans

A Java Bean is a reusable software component that follows a specific set of conventions.…

3 weeks ago

Java String Methods

Java provides a rich set of built-in methods for handling String operations efficiently. Since strings…

3 weeks ago

Java Developer Jobs

Java remains one of the most in-demand programming languages worldwide, powering everything from enterprise applications…

3 weeks ago

Converting Integer to String in Java

Java provides multiple ways to convert an integer (int) to a string (String). Whether you're…

3 weeks ago

JSP to JSP Communication: A Complete Guide to Dynamic Java Web Development

Java Server Pages (JSP) is a powerful technology used to develop dynamic web applications by…

4 weeks ago