This Java JDBC CRUD tutorial will teach you how to do basic database functions that are CREATE RETIEVE UPDATE and DELETE and SEARCH 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 and SEARCH in database by writing code to manage the products table in the database named smobile. products table consist of following columns product name,price ,qty.
we will do the java best practice for understand java programming clearly and easily.
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.
Step 3: Download xampp server Click here follow the steps and install it.
you have to install xampp server in your computer Click here.after that you shall be able to create datebase.
To connect the database in Java please refer the following code. To create the method name connect (); paste the below code inside the Connect () method.
Connection con; //create the connection object con PreparedStatement pst; // create the PreparedStatement object pst ResultSet rs; // create the ResultSet object rs Statement stmt; // create the Statement object stmt public void Connect() { try { Class.forName("com.mysql.jdbc.Driver"); con = DriverManager.getConnection("jdbc:mysql://localhost/smobile", "root", ""); } catch (ClassNotFoundException ex) { ex.printStackTrace(); } catch (SQLException ex) { ex.printStackTrace(); } }
you can use the following code snippet to add the records in to database. paste the code inside the add button
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String pname = txtpname.getText(); String price = txtprice.getText(); String qty = txtqty.getText(); try { pst = con.prepareStatement("insert into products(pname,price,qty)values(?,?,?)"); pst.setString(1, pname); pst.setString(2, price); pst.setString(3, qty); int k = pst.executeUpdate(); if(k==1) { JOptionPane.showMessageDialog(this, "Record Adddeddd"); txtpname.setText(""); txtprice.setText(""); txtqty.setText(""); txtpname.requestFocus(); LoadProductno(); } else { JOptionPane.showMessageDialog(this, "Record Failed"); } } catch (SQLException ex) { Logger.getLogger(products.class.getName()).log(Level.SEVERE, null, ex); } }
you can use the following code snippet to load the product id from the database. you have to create the function LoadProductno().paste the code inside the LoadProductno() as i shown the below of code.
public void LoadProductno() { try { pst = con.prepareStatement("SELECT id FROM products"); rs = pst.executeQuery(); txtpid.removeAllItems(); while (rs.next()) { txtpid.addItem(rs.getString(1)); } } catch ( Exception e) { e.printStackTrace(); } }
Select the relavent product id from the combobox and click on search button data will be displayed on the relavent textfields. attached the screen shot image above.
you can use the following code snippet to Search the records.
private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) { String pid = txtpid.getSelectedItem().toString(); try { pst = con.prepareStatement( "SELECT * FROM products where id = ?" ); pst.setString(1, pid); rs = pst.executeQuery(); if ( rs.next() == true ) { txtpname.setText(rs.getString(2)); txtprice.setText(rs.getString(3)); txtqty.setText(rs.getString(4)); } else { JOptionPane.showMessageDialog(this, "Record Not Found"); } } catch ( Exception e ) { e.printStackTrace(); } }
you can use the following code snippet to update the records.
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) { String pname = txtpname.getText(); String price = txtprice.getText(); String qty = txtqty.getText(); String pid = txtpid.getSelectedItem().toString(); try { pst = con.prepareStatement("update products set pname = ?, price = ?, qty= ? where id = ?"); pst.setString(1, pname); pst.setString(2, price); pst.setString(3, qty); pst.setString(4, pid); int k = pst.executeUpdate(); if(k==1) { JOptionPane.showMessageDialog(this, "Record Updatedddddddd"); txtpname.setText(""); txtprice.setText(""); txtqty.setText(""); txtpname.requestFocus(); LoadProductno(); } else { JOptionPane.showMessageDialog(this, "Record Failed"); } } catch (SQLException ex) { Logger.getLogger(products.class.getName()).log(Level.SEVERE, null, ex); } }
you can use the following code snippet to Delete the records.
private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) { try { String pid = txtpid.getSelectedItem().toString(); pst = con.prepareStatement("delete from products where id =?"); pst.setString(1, pid); int k = pst.executeUpdate(); if(k==1) { JOptionPane.showMessageDialog(this, "Record Deleteedddddd"); txtpname.setText(""); txtprice.setText(""); txtqty.setText(""); txtpname.requestFocus(); LoadProductNo(); } else { JOptionPane.showMessageDialog(this, "Record Failed"); } } catch (SQLException ex) { Logger.getLogger(Product.class.getName()).log(Level.SEVERE, null, ex); } }
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
Act as a Java developer to create a program that calculates the gross wage for…
Initialize the employee number, Hourswork,and Hoursrate to calculate a grosswage use the following condition. if…
In this tutorial, we will teach you how to create a simple school management system…
I have design the Admin Basic templete using React MUI Design Admin Dashboard and Login.Here…
In this tutorial ,i am to going teach the Laravel Breeze.Laravel Breeze provides a simple…