Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

How to perform update sql query in a Java application using JDBC ?



A simple Java application showing how to query a table in a database i.e performing a update query using JDBC

Steps for querying a Database using JDBC -

1. Download the driver jar from Internet and place it in classpath of your Java application. Here I am using MySql as Database so I have downloaded the jar with name as : "mysql-connector-java-5.1.20-bin.jar"

2. Create a Database (am using MySql as backend) name as "javaee".

3. Create a table in "javaee" database as "customer". Run the create query stated below -

CREATE TABLE customer (
   'First Name' varchar(30) ,
   'Last Name' varchar(30) ,
   'E-Mail' varchar(45) ,
   'City' varchar(30) ,
   'password' varchar(30) NOT NULL,
   PRIMARY KEY ('password')
 ) 



4. Populate the table user with some data (of your own), if not run the four insert queries stated below -

insert into customer () values ('Jonty','Magic','jonty@magic.com','Pune','123456');

insert into customer () values ('Java','Sun','java@sun.com','New York','456');

insert into customer () values ('Jesse','lool','jesse@lool.com','Jamaica','23456');

insert into customer () values ('Cameroon','Black','cameroon@black.com','Sydney','34');



5. Create a Java Class. It will perform necessary insert query. The code for Java class is shown below -


package com.hubberspot.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class UpdateJdbcQuery {

 Connection connection = null;
 ResultSet resultSet = null;
 Statement statement = null;
 static String query = null;
 String url = null;
 String username = null;
 String password = null;

 public static void main(String[] args) {

  UpdateJdbcQuery ujq = new UpdateJdbcQuery();
  ujq.createConnection();

  query = "update customer set Last_Name='Smarty' , First_Name='John' , " +
    "Email='john@smarty.com' where First_Name='Jonty';";

  ujq.executeQuery(query);

  query = "update customer set Last_Name='Tommy' , First_Name='Hil' , " +
    "Email='hil@tommy.com' where First_Name='Jesse';";

  ujq.executeQuery(query);


  query = "update customer set Last_Name='Tendulkar' , First_Name='Sachin' , " +
    "Email='sachin@god.com' where First_Name='Cameroon';";

  ujq.executeQuery(query);

  query = "update customer set Last_Name='Sharp' , First_Name='See' , " +
    "Email='see@sharp.com' where First_Name='Java';";

  ujq.executeQuery(query);

  ujq.release();

 }

 private void release() {

  try {    
   statement.close();
   connection.close();
  }
  catch (SQLException se) {

   se.printStackTrace();
  } 


 }

 private void createConnection() {

  url = "jdbc:mysql://localhost:3306/javaee";
  username = "root"; 
  password = "root"; 
  try {
   Class.forName("com.mysql.jdbc.Driver").newInstance();

   connection = DriverManager.getConnection(url, username , password);
  }
  catch (Exception e) {

   e.printStackTrace();
  }


 }

 private void executeQuery(String query) {

  try {

   statement = connection.createStatement();
   statement.execute(query);

  }
  catch (SQLException e) {

   e.printStackTrace();

  }
 } 
}





Output after Insert query -











Output after Update query -


 
© 2021 Learn Java by Examples Template by Hubberspot