JDBC Practise Programs
JDBC Practise Programs
Syllabus :
****************************************************************************************************
Programs
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
try {
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
//Statement stmt=con.createStatement();
String query = "SELECT first_name, last_name FROM customers LIMIT
?";
PreparedStatement preparedStatement = con.prepareStatement(query);
//int limit=10;
preparedStatement.setInt(1, 10);
System.out.println("getting the data");
ResultSet rs=preparedStatement.executeQuery();
while(rs.next()) {
String name=rs.getString("first_name");
String lname=rs.getString("last_name");
//printing name
System.out.print("Name : " + name);
System.out.print(" Last Name : " + lname);
System.out.println(" |");
}
// Close the resources
rs.close();
//stmt.close();
con.close();
preparedStatement.close();
}catch (SQLException e) {
e.printStackTrace();
}}}
Scrollable ResultSet with next(), last(), previous(), at a absolute position 3 with a LIMIT
of 10
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
try {
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
//Statement stmt=con.createStatement();
String query = "SELECT first_name, last_name FROM customers LIMIT
?";
PreparedStatement preparedStatement = con.prepareStatement(query,
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
//int limit=10;
preparedStatement.setInt(1, 10);
System.out.println("getting the data");
ResultSet rs=preparedStatement.executeQuery();
System.out.println("******************ResultSet Move
Forward*********************");
while(rs.next()) {
String name=rs.getString("first_name");
String lname=rs.getString("last_name");
//printing name
System.out.print("Name : " + name);
System.out.print(" Last Name : " + lname);
System.out.println(" |");
}
System.out.println("******************ResultSet At last*********************");
//rs.last();
System.out.println("******************ResultSet Move
Backward*********************");
while(rs.previous()) {
String name=rs.getString("first_name");
String lname=rs.getString("last_name");
//printing name
System.out.print("Name : " + name);
System.out.print(" Last Name : " + lname);
System.out.println(" |");
}
System.out.println("******************ResultSet At Position
3*********************");
rs.absolute(3);
String name=rs.getString("first_name");
String lname=rs.getString("last_name");
//printing name
System.out.print("Name : " + name);
System.out.print(" Last Name : " + lname);
System.out.println(" |");
// Close the resources
rs.close();
//stmt.close();
con.close();
preparedStatement.close();
}catch (SQLException e) {
e.printStackTrace();
}
O/P-
connecting to database
getting the data
******************ResultSet Move Forward*********************
Name : Babara Last Name : MacCaffrey |
Name : Ines Last Name : Brushfield |
Name : Freddi Last Name : Boagey |
Name : Ambur Last Name : Roseburgh |
Name : Clemmie Last Name : Betchley |
Name : Elka Last Name : Twiddell |
Name : Ilene Last Name : Dowson |
Name : Thacher Last Name : Naseby |
Name : Romola Last Name : Rumgay |
Name : Levy Last Name : Mynett |
******************ResultSet At last*********************
******************ResultSet Move Backward*********************
Name : Levy Last Name : Mynett |
Name : Romola Last Name : Rumgay |
Name : Thacher Last Name : Naseby |
Name : Ilene Last Name : Dowson |
Name : Elka Last Name : Twiddell |
Name : Clemmie Last Name : Betchley |
Name : Ambur Last Name : Roseburgh |
Name : Freddi Last Name : Boagey |
Name : Ines Last Name : Brushfield |
Name : Babara Last Name : MacCaffrey |
******************ResultSet At Position 3*********************
Name : Freddi Last Name : Boagey |
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
try {
//Class.forName("com.mysql.jdbc.Driver");
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
//Statement stmt=con.createStatement();
String query = "SELECT * FROM customers LIMIT ?";
PreparedStatement preparedStatement = con.prepareStatement(query,
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
//int limit=10;
preparedStatement.setInt(1, 10);
System.out.println("getting the data");
ResultSet rs=preparedStatement.executeQuery();
rs.moveToInsertRow();
rs.updateString("first_name", "pinku");
rs.updateString("last_name", "Gaykwad");
rs.updateInt("phone", 777777777);
rs.updateString("birth_date", "2023-04-05");
rs.updateString("address", "buldhana");
rs.updateString("city", "buldhan");
rs.updateString("state", "dd");
rs.updateInt("points", 1000);
rs.insertRow();
}
private static void moveToInsertRow() {
// TODO Auto-generated method stub
}}
Inserted into database :
16 pinku Gaykwad 2023-04-05 77777777 buldhan buldhan dd 1000
7 a
Program to update a phone number and points by using absolute method with the help of
preparedStatement
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
ResultSet rs=preparedStatement.executeQuery();
rs.absolute(14);
rs.updateInt("points", 10000);
rs.updateString("phone", "11111111111");
rs.updateRow();
System.out.println("Phone number & points updated successfully.");
// Close the resources
rs.close();
//stmt.close();
con.close();
preparedStatement.close();
}catch (SQLException e) {
e.printStackTrace();
}
O/P-
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
System.out.println("Inserting data");
preparedStatement.setInt(1, 11);
preparedStatement.setString(2, "Wire");
preparedStatement.setInt(3, 50);
preparedStatement.setDouble(4, 6.6);
int NumberOfRowInserted=preparedStatement.executeUpdate();
System.out.println("Number of inseted row : "+ NumberOfRowInserted);
//stmt.close();
con.close();
preparedStatement.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
O/P-
connecting to database
Inserting data
Number of inseted row : 1
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
System.out.println("Updating data");
preparedStatement.setString(1, "9822200930");
int NumberOfRowInserted=preparedStatement.executeUpdate();
System.out.println("Number of inseted row : "+ NumberOfRowInserted);
con.close();
preparedStatement.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
O/P-
connecting to database
Updating data
Number of inseted row : 1
package JdbcConnectionDemo;
import java.io.PrintStream;
import java.sql.*;
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ResultSet rs=ps.executeQuery();
while(rs.next()) {
int Pid=rs.getInt(1);
String name=rs.getString(2);
int qtyStock=rs.getInt(3);
double price=rs.getDouble(4);
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store",
"root", "123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
ps.setInt(1, 16);
System.out.println("Executing query");
int DeleteRow=ps.executeUpdate();
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
Program for LIKE clause demo, getting dat from user and like single char and obtaining data
which matches user input
package JdbcConnectionDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JdbcDemo {
public static void main(String[] args) {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ResultSet rs=ps.executeQuery();
while(rs.next()) {
int Pid=rs.getInt(1);
String name=rs.getString(2);
int qtyStock=rs.getInt(3);
double price=rs.getDouble(4);
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
O/P
Enter the Name :L
connecting to database
creating connection
Executing query
Product ID : 3
Product Name : Lettuce - Romaine, Heart
Qty In stock : 38
Price : 3.35
*****************************
Product ID : 9
Product Name : Longan
Qty In stock : 67
Price : 2.26
*****************************
Enter the Name : a
connecting to database
creating connection
Executing query
Enter the Name : B
connecting to database
creating connection
Executing query
Product ID : 4
Product Name : Brocolinni - Gaylan, Chinese
Qty In stock : 90
Price : 4.53
*****************************
Product ID : 10
Product Name : Broom - Push
Qty In stock : 6
Price : 1.09
*****************************
Enter the Name : S
connecting to database
creating connection
Executing query
Product ID : 5
Product Name : Sauce - Ranch Dressing
Qty In stock : 94
Price : 1.63
*****************************
Product ID : 7
Product Name : Sweet Pea Sprouts
Qty In stock : 98
Price : 3.29
*****************************
Program for sorting the data by Ascending & order and descending order
package JdbcConnectionDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JdbcDemo {
public static void main(String[] args) {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
String query;
if(orderBy.equalsIgnoreCase("Ascending")) {
query="SELECT * FROM products ORDER BY name ASC";
}
else {
query="SELECT * FROM products ORDER BY name
DESC";
}
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ResultSet rs=ps.executeQuery();
while(rs.next()) {
int Pid=rs.getInt(1);
String name=rs.getString(2);
int qtyStock=rs.getInt(3);
double price=rs.getDouble(4);
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();}}}
O/p-
Enter the Accesding or Descending Enter the Accesding or Descending
ascending descending
connecting to database connecting to database
creating connection creating connection
Executing query Executing query
Product ID : 4 Product ID : 11
Product Name : Brocolinni - Gaylan, Chinese Product Name : Wire
Qty In stock : 90 Qty In stock : 50
Price : 4.53 Price : 6.6
***************************** *****************************
Product ID : 10 Product ID : 7
Product Name : Broom - Push Product Name : Sweet Pea Sprouts
Qty In stock : 6 Qty In stock : 98
Price : 1.09 Price : 3.29
***************************** *****************************
Product ID : 1 Product ID : 5
Product Name : Foam Dinner Plate Product Name : Sauce - Ranch Dressing
Qty In stock : 70 Qty In stock : 94
Price : 1.21 Price : 1.63
***************************** *****************************
Product ID : 8 Product ID : 2
Product Name : Island Oasis - Raspberry Product Name : Pork - Bacon,back Peameal
Qty In stock : 26 Qty In stock : 49
Price : 0.74 Price : 4.65
***************************** *****************************
Product ID : 3 Product ID : 6
Product Name : Lettuce - Romaine, Heart Product Name : Petit Baguette
Qty In stock : 38 Qty In stock : 14
Price : 3.35 Price : 2.39
***************************** *****************************
Product ID : 9 Product ID : 9
Product Name : Longan Product Name : Longan
Qty In stock : 67 Qty In stock : 67
Price : 2.26 Price : 2.26
***************************** *****************************
Product ID : 6 Product ID : 3
Product Name : Petit Baguette Product Name : Lettuce - Romaine, Heart
Qty In stock : 14 Qty In stock : 38
Price : 2.39 Price : 3.35
***************************** *****************************
Product ID : 2 Product ID : 8
Product Name : Pork - Bacon,back Peameal Product Name : Island Oasis - Raspberry
Qty In stock : 49 Qty In stock : 26
Price : 4.65 Price : 0.74
***************************** *****************************
Product ID : 5 Product ID : 1
Product Name : Sauce - Ranch Dressing Product Name : Foam Dinner Plate
Qty In stock : 94 Qty In stock : 70
Price : 1.63 Price : 1.21
***************************** *****************************
Product ID : 7 Product ID : 10
Product Name : Sweet Pea Sprouts Product Name : Broom - Push
Qty In stock : 98 Qty In stock : 6
Price : 3.29 Price : 1.09
***************************** *****************************
Product ID : 11 Product ID : 4
Product Name : Wire Product Name : Brocolinni - Gaylan, Chinese
Qty In stock : 50 Qty In stock : 90
Price : 6.6 Price : 4.53
***************************** ******************************
package JdbcConnectionDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class JdbcDemo {
public static void main(String[] args) {
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ps.executeUpdate();
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();
}
}}
O/P-
connecting to database
creating connection
Executing query
Database created successfully!!!
package JdbcConnectionDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcDemo {
public static void main(String[] args) {
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/sql_store", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ps.executeUpdate();
System.out.println("Database Drop successfully!!!");
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();}}}
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ps.executeUpdate();
//rs.close();
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
O/p-
connecting to database
creating connection
Executing query
Table created successfully!!!
package JdbcConnectionDemo;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class JdbcDemo {
public static void main(String[] args) {
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement ps=con.prepareStatement(query);
System.out.println("Executing query");
ps.executeUpdate();
con.close();
ps.close();
}catch (SQLException e) {
e.printStackTrace();}}}
O/P- connecting to database
creating connection
Executing query
Table Drop successfully!!!
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root",
"123123azAZ@");
con.setAutoCommit(false);
System.out.println("creating connection");
Statement s=con.createStatement();
System.out.println("Executing query");
s.addBatch(query);
s.addBatch(query1);
s.addBatch(query2);
int batch[]=s.executeBatch();
con.commit();
for(int i=0; i<=batch.length; i++) {
System.out.println("I am Updating value in database, I
updated this : "+batch[i]);
}
e.printStackTrace();
}
}
}
O/p- connecting to database
creating connection
Executing query
I am Updating value in database, I updated this : 1
I am Updating value in database, I updated this : 1
I am Updating value in database, I updated this : 1
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root",
"123123azAZ@");
con.setAutoCommit(false);
System.out.println("creating connection");
PreparedStatement s=con.prepareStatement(query);
System.out.println("Executing query");
s.setInt(1, 12);
s.setString(2, "Sophia");
s.setString(3, "Davis");
s.setString(4, "sophia.davis@example.com");
s.setString(5, "5557891234");
s.addBatch();
int batch[]=s.executeBatch();
con.commit();
for(int i=0; i<=batch.length; i++) {
System.out.println("I am Updating value in database, I updated
this : "+batch[i]);
}
con.close();
s.close();
}catch (SQLException e) {
e.printStackTrace();
}}}
O/P-connecting to database
creating connection
Executing query
I am Updating value in database, I updated this : 1
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root",
"123123azAZ@");
System.out.println("creating connection");
PreparedStatement s=con.prepareStatement(query);
s.setBytes(1, fileData);
System.out.println("Executing query");
s.executeUpdate();
e.printStackTrace();
}
}
}
try {
System.out.println("connecting to database");
Connection
con=DriverManager.getConnection("jdbc:mysql://localhost:3306/employee", "root",
"123123azAZ@");
con.setAutoCommit(false);
Statement s=con.createStatement();
System.out.println("creating connection");
System.out.println("Executing query");
s.executeUpdate(query1);
s.executeUpdate(query2);
s.executeUpdate(query3);
con.commit();
con.rollback();
con.close();
s.close();
}catch (SQLException e) {
e.printStackTrace();
}
}
}
o/p- connecting to database
creating connection
Executing query
All entry inserted successfully!!!