SlideShare a Scribd company logo
JDBC
(Java Database Connectivity)
Overview (1/2)
 JDBC
 JDBC is a standard interface for connecting to relational databases
from Java
 The JDBC Classes and Interfaces are in the java.sql package
 JDBC is Java API for executing SQL statements

Provides a standard API for tool/database developers

Possible to write database applications using a pure Java API

Easy to send SQL statements to virtually any relational database
 What does JDBC do?
 Establish a connection with a database
 Send SQL statements
 Process the results
JDBC Driver
JAVA Applet/
Application Database
JDBC Call
Database
Command
 Reason for JDBC
 Database vendors (Microsoft Access, Oracle etc.) provide
proprietary (non standard) API for sending SQL to the server
and receiving results from it
 Languages such as C/C++ can make use of these proprietary
APIs directly

High performance

Can make use of non standard features of the database

All the database code needs to be rewritten if you change
database vendor or product
 JDBC is a vendor independent API for accessing relational
data from different database vendors in a consistent way
CCTM: Course material developed by James King (james.king@londonmet.ac.uk)
Overview (2/2)
History of JDBC (1/2)
 JDBC 1.0 released 9/1996.
 Contains basic functionality to connect to database, query database,
process results
 JDBC classes are part of java.sql package
 Comes with JDK 1.1
 JDBC 2.0 released 5/1998
 Comes with JDK 1.2
 javax.sql contains additional functionality
 Additional functionality:

Scroll in result set or move to specific row

Update database tables using Java methods instead of SQL
commands

Send multiple SQL statements to the database as a batch

Use of SQL3 datatypes as column values
History of JDBC (2/2)
 JDBC 3.0 released 2/2002
 Comes with Java 2, J2SE 1.4

Support for:

Connection pooling

Multiple result sets

Prepared statement pooling

Save points in transactions
JDBC Model
 JDBC consists of two parts:
 JDBC API, a purely Java-based API
 JDBC driver manager

Communicates with vendor-specific
drivers
 Connection con =
DriverManager.getConnection( "jd
bc:myDriver:myDatabase",
username, password);
JAVA Applet/
Application
JDBC API
Driver Manager
Driver API
Vendor Specific
JDBC Driver
JDBC-ODBC Bridge
Database
Vender Specific
ODBC Driver
Database
Java Application
Developer
JDBC Developer
Vender Specific
JDBC developer
JDBC Programming Steps
Connect
Query
Process Results
Close
1) Register the driver
2) Create a connection to the database
1) Create a statement
2) Query the database
1) Get a result set
2) Assign results to Java variables
1) Close the result set
2) Close the statement
3) Close the connection
Skeleton Code
Class.forName(DRIVERNAME);
Connection con = DriverManager.getConnection(
CONNECTIONURL, DBID, DBPASSWORD);
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(“SELECT a, b, c FROM member);
While(rs.next())
{
Int x = rs.getInt(“a”);
String s = rs.getString(“b”);
Float f = rs.getFloat(“c”);
}
rs.close();
stmt.close();
con.close();
Loading a JDBC driver
Connecting to a database
Processing the result set
Closing the connections
Executing SQL
Step 1 : Loading a JDBC Driver
 A JDBC driver is needed to connect to a database
 Loading a driver requires the class name of the driver.
Ex) JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver
Oracle driver: oracle.jdbc.driver.OracleDriver
MySQL: com.mysql.jdbc.Driver
 Loaing the driver class
Class.forName("com.mysql.jdbc.Driver");
 It is possible to load several drivers.
 The class DriverManager manages the loaded driver(s)
Step 2 : Connecting to a Database (1/2)
 JDBC URL for a database
 Identifies the database to be connected
 Consists of three-part:
jdbc:<subprotocol>:<subname>
Protocol: JDBC is
the only protocol in
JDBC
Protocol: JDBC is
the only protocol in
JDBC
Subname: indicates the location and
name of the database to be
accessed. Syntax is driver specific
Subname: indicates the location and
name of the database to be
accessed. Syntax is driver specific
Sub-protocol:
identifies a
database
driver
Sub-protocol:
identifies a
database
driver
Ex) jdbc:mysql://oopsla.snu.ac.kr/mydb
The syntax for the name of the database is a little messy and is
unfortunately vendor specific
JDBC URL
Vendor of database, Location of
database server and name of
database
Username Password
Step 2 : Connecting to a Database (2/2)
 The DriverManager allows you to connect to a database using
the specified JDBC driver, database location, database name,
username and password.
 It returns a Connection object which can then be used to
communicate with the database.
Connection connection =
DriverManager.getConnection("jdbc:mysql://oopsla.snu.ac.kr/mydb",“useri
d",“password");
JDBC URL
Vendor of database, Location of
database server and name of
database
Username Password
Step 3 : Executing SQL (1/2)
 Statement object
 Can be obtained from a Connection object
 Sends SQL to the database to be executed
 Statement has three methods to execute a SQL statement:
 executeQuery() for QUERY statements

Returns a ResultSet which contains the query results
 executeUpdate() for INSERT, UPDATE, DELETE statements

Returns an integer, the number of affected rows from the SQL
 execute() for either type of statement
Statement statement = connection.createStatement();
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery
("select RENTAL_ID, STATUS from ACME_RENTALS");
Statement stmt = conn.createStatement();
int rowcount = stmt.executeUpdate
("delete from ACME_RENTAL_ITEMS
where rental_id = 1011");
Step 3 : Executing SQL (2/2)
 Execute a select statement
 Execute a delete statement
Step 4 : Processing the Results (1/2)
 JDBC returns the results of a query in a ResultSet object
 ResultSet object contains all of the rows which satisfied the conditions
in an SQL statement
 A ResultSet object maintains a cursor pointing to its current
row of data
 Use next() to step through the result set row by row

next() returns TRUE if there are still remaining records
 getString(), getInt(), and getXXX() assign each value to a Java
variable
Record 1 Record 2 Record 3 Record 4
ResultSetInternal Pointer
The internal pointer starts one before the first record
Step 4 : Processing the Results (2/2)
 Example
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(“SELECT ID, name, score FROM table1”);
While (rs.next()){
int id = rs.getInt(“ID”);
String name = rs.getString(“name”);
float score = rs.getFloat(“score”);
System.out.println(“ID=” + id + “ ” + name + “ ” + score);}
NOTE
You must step the cursor to the first record before read the results
This code will not skip the first record
ID name score
1 James 90.5
2 Smith 45.7
3 Donald 80.2
Table1
Output
ID=1 James 90.5
ID=2 Smith 45.7
ID=3 Donald 80.2
Step 5 : Closing Database Connection
 It is a good idea to close the Statement and Connection objects
when you have finished with them
 Close the ResultSet object
rs.close();
 Close the Statement object
stmt.close();
 Close the connection
connection.close();
The PreparedStatement Object
 A PreparedStatement object holds precompiled SQL
statements
 Use this object for statements you want to execute more than
once
 A PreparedStatement can contain variables (?) that you supply
each time you execute the statement
// Create the prepared statement
PreparedStatement pstmt = con.prepareStatement(“
UPDATE table1 SET status = ? WHERE id =?”)
// Supply values for the variables
pstmt.setString (1, “out”);
pstmt.setInt(2, id);
// Execute the statement
pstmt.executeUpdate();

More Related Content

PPT
Jdbc complete
Sandeep Rawat
 
PPT
SQLITE Android
Sourabh Sahu
 
PPTX
Ado.Net Tutorial
prabhu rajendran
 
PPT
Joins in SQL
Vigneshwaran Sankaran
 
PPTX
Sql queries presentation
NITISH KUMAR
 
PPT
Sql ppt
Anuja Lad
 
PDF
SQL Overview
Stewart Rogers
 
Jdbc complete
Sandeep Rawat
 
SQLITE Android
Sourabh Sahu
 
Ado.Net Tutorial
prabhu rajendran
 
Joins in SQL
Vigneshwaran Sankaran
 
Sql queries presentation
NITISH KUMAR
 
Sql ppt
Anuja Lad
 
SQL Overview
Stewart Rogers
 

What's hot (20)

PPTX
SQL commands
GirdharRatne
 
PPTX
Relational database
Megha Sharma
 
PPTX
Normalization in DBMS
Prateek Parimal
 
ODP
Android App Development - 05 Action bar
Diego Grancini
 
PPTX
SQL Basics
Hammad Rasheed
 
PPT
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
PPS
Jdbc architecture and driver types ppt
kamal kotecha
 
PPTX
Triggers
Pooja Dixit
 
PPTX
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
PPTX
Presentation on Core java
mahir jain
 
PDF
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
Edureka!
 
PPTX
Sql commands
Pooja Dixit
 
PPT
DBMS PPT
Prabhu Goyal
 
DOC
Dbms lab questions
Parthipan Parthi
 
PDF
Layouts in android
Durai S
 
PPT
Association rule mining
Acad
 
PPTX
NOSQL Databases types and Uses
Suvradeep Rudra
 
PPSX
JDBC: java DataBase connectivity
Tanmoy Barman
 
PPT
Introduction to MongoDB
Ravi Teja
 
SQL commands
GirdharRatne
 
Relational database
Megha Sharma
 
Normalization in DBMS
Prateek Parimal
 
Android App Development - 05 Action bar
Diego Grancini
 
SQL Basics
Hammad Rasheed
 
JDBC,Types of JDBC,Resultset, statements,PreparedStatement,CallableStatements...
Pallepati Vasavi
 
Jdbc architecture and driver types ppt
kamal kotecha
 
Triggers
Pooja Dixit
 
Chapter 1 introduction to sql server
baabtra.com - No. 1 supplier of quality freshers
 
Presentation on Core java
mahir jain
 
MySQL Tutorial For Beginners | Relational Database Management System | MySQL ...
Edureka!
 
Sql commands
Pooja Dixit
 
DBMS PPT
Prabhu Goyal
 
Dbms lab questions
Parthipan Parthi
 
Layouts in android
Durai S
 
Association rule mining
Acad
 
NOSQL Databases types and Uses
Suvradeep Rudra
 
JDBC: java DataBase connectivity
Tanmoy Barman
 
Introduction to MongoDB
Ravi Teja
 
Ad

Viewers also liked (11)

PPT
Jdbc slide for beginers
Ambarish Rai
 
PDF
Weather patterns
Highline Academy
 
PDF
Spring framework - J2EE S Lidskou Tvari
Roman Pichlík
 
PPTX
Java.sql package
myrajendra
 
PPT
JDBC
Ankit Desai
 
PPT
JDBC Tutorial
Information Technology
 
PPT
Jdbc ppt
Vikas Jagtap
 
PPT
KMUTNB - Internet Programming 6/7
phuphax
 
PPS
Abzolute Logistic Solution
phuphax
 
PPT
1 java servlets and jsp
Ankit Minocha
 
Jdbc slide for beginers
Ambarish Rai
 
Weather patterns
Highline Academy
 
Spring framework - J2EE S Lidskou Tvari
Roman Pichlík
 
Java.sql package
myrajendra
 
JDBC Tutorial
Information Technology
 
Jdbc ppt
Vikas Jagtap
 
KMUTNB - Internet Programming 6/7
phuphax
 
Abzolute Logistic Solution
phuphax
 
1 java servlets and jsp
Ankit Minocha
 
Ad

Similar to Jdbc (database in java) (20)

PPTX
JDBC
Balwinder Kumar
 
PPT
jdbc_presentation.ppt
DrMeenakshiS
 
PPT
JDBC.ppt
Jayaprasanna4
 
PDF
Jdbc
mishaRani1
 
PDF
Jdbc[1]
Fulvio Corno
 
PDF
JDBC programming
Fulvio Corno
 
PPTX
Java Data Base Connectivity concepts.pptx
mukeshprasanth909
 
PPTX
03-JDBC.pptx
HachaluHaile
 
PPT
JDBC java for learning java for learn.ppt
kingkolju
 
PPT
Java jdbc
Arati Gadgil
 
PPTX
Jdbc presentation
nrjoshiee
 
PDF
Jdbc 1
Tuan Ngo
 
PDF
Introduction to JDBC and database access in web applications
Fulvio Corno
 
PPTX
Jdbc introduction
Rakesh Kumar Ray
 
PDF
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
anuwaradisu19
 
PPT
Jdbc sasidhar
Sasidhar Kothuru
 
PPTX
Jdbc Java Programming
chhaichivon
 
PPTX
Jdbc
DeepikaT13
 
jdbc_presentation.ppt
DrMeenakshiS
 
JDBC.ppt
Jayaprasanna4
 
Jdbc[1]
Fulvio Corno
 
JDBC programming
Fulvio Corno
 
Java Data Base Connectivity concepts.pptx
mukeshprasanth909
 
03-JDBC.pptx
HachaluHaile
 
JDBC java for learning java for learn.ppt
kingkolju
 
Java jdbc
Arati Gadgil
 
Jdbc presentation
nrjoshiee
 
Jdbc 1
Tuan Ngo
 
Introduction to JDBC and database access in web applications
Fulvio Corno
 
Jdbc introduction
Rakesh Kumar Ray
 
Chapter 5 JDBC.pdf for stufent of computer andtudent It s
anuwaradisu19
 
Jdbc sasidhar
Sasidhar Kothuru
 
Jdbc Java Programming
chhaichivon
 

Recently uploaded (20)

PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PDF
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PPTX
Edge to Cloud Protocol HTTP WEBSOCKET MQTT-SN MQTT.pptx
dhanashri894551
 
PPTX
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
PPTX
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
B.Tech Data Science Program (Industry Integrated ) Syllabus
rvray078
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PPTX
Azure-DevOps-Training presentation downloadable
NamanGoyal428595
 
PDF
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
PPTX
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
PDF
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
PPTX
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PPTX
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
BRKDCN-2613.pdf Cisco AI DC NVIDIA presentation
demidovs1
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Edge to Cloud Protocol HTTP WEBSOCKET MQTT-SN MQTT.pptx
dhanashri894551
 
Production of bioplastic from fruit peels.pptx
alwingeorgealwingeor
 
ANIMAL INTERVENTION WARNING SYSTEM (4).pptx
dodultrongaming
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
B.Tech Data Science Program (Industry Integrated ) Syllabus
rvray078
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
Azure-DevOps-Training presentation downloadable
NamanGoyal428595
 
Chad Ayach - A Versatile Aerospace Professional
Chad Ayach
 
Chapter_Seven_Construction_Reliability_Elective_III_Msc CM
SubashKumarBhattarai
 
6th International Conference on Artificial Intelligence and Machine Learning ...
gerogepatton
 
AgentX UiPath Community Webinar series - Delhi
RohitRadhakrishnan8
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Unit 5 BSP.pptxytrrftyyydfyujfttyczcgvcd
ghousebhasha2007
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 

Jdbc (database in java)

  • 2. Overview (1/2)  JDBC  JDBC is a standard interface for connecting to relational databases from Java  The JDBC Classes and Interfaces are in the java.sql package  JDBC is Java API for executing SQL statements  Provides a standard API for tool/database developers  Possible to write database applications using a pure Java API  Easy to send SQL statements to virtually any relational database  What does JDBC do?  Establish a connection with a database  Send SQL statements  Process the results JDBC Driver JAVA Applet/ Application Database JDBC Call Database Command
  • 3.  Reason for JDBC  Database vendors (Microsoft Access, Oracle etc.) provide proprietary (non standard) API for sending SQL to the server and receiving results from it  Languages such as C/C++ can make use of these proprietary APIs directly  High performance  Can make use of non standard features of the database  All the database code needs to be rewritten if you change database vendor or product  JDBC is a vendor independent API for accessing relational data from different database vendors in a consistent way CCTM: Course material developed by James King ([email protected]) Overview (2/2)
  • 4. History of JDBC (1/2)  JDBC 1.0 released 9/1996.  Contains basic functionality to connect to database, query database, process results  JDBC classes are part of java.sql package  Comes with JDK 1.1  JDBC 2.0 released 5/1998  Comes with JDK 1.2  javax.sql contains additional functionality  Additional functionality:  Scroll in result set or move to specific row  Update database tables using Java methods instead of SQL commands  Send multiple SQL statements to the database as a batch  Use of SQL3 datatypes as column values
  • 5. History of JDBC (2/2)  JDBC 3.0 released 2/2002  Comes with Java 2, J2SE 1.4  Support for:  Connection pooling  Multiple result sets  Prepared statement pooling  Save points in transactions
  • 6. JDBC Model  JDBC consists of two parts:  JDBC API, a purely Java-based API  JDBC driver manager  Communicates with vendor-specific drivers  Connection con = DriverManager.getConnection( "jd bc:myDriver:myDatabase", username, password); JAVA Applet/ Application JDBC API Driver Manager Driver API Vendor Specific JDBC Driver JDBC-ODBC Bridge Database Vender Specific ODBC Driver Database Java Application Developer JDBC Developer Vender Specific JDBC developer
  • 7. JDBC Programming Steps Connect Query Process Results Close 1) Register the driver 2) Create a connection to the database 1) Create a statement 2) Query the database 1) Get a result set 2) Assign results to Java variables 1) Close the result set 2) Close the statement 3) Close the connection
  • 8. Skeleton Code Class.forName(DRIVERNAME); Connection con = DriverManager.getConnection( CONNECTIONURL, DBID, DBPASSWORD); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(“SELECT a, b, c FROM member); While(rs.next()) { Int x = rs.getInt(“a”); String s = rs.getString(“b”); Float f = rs.getFloat(“c”); } rs.close(); stmt.close(); con.close(); Loading a JDBC driver Connecting to a database Processing the result set Closing the connections Executing SQL
  • 9. Step 1 : Loading a JDBC Driver  A JDBC driver is needed to connect to a database  Loading a driver requires the class name of the driver. Ex) JDBC-ODBC: sun.jdbc.odbc.JdbcOdbcDriver Oracle driver: oracle.jdbc.driver.OracleDriver MySQL: com.mysql.jdbc.Driver  Loaing the driver class Class.forName("com.mysql.jdbc.Driver");  It is possible to load several drivers.  The class DriverManager manages the loaded driver(s)
  • 10. Step 2 : Connecting to a Database (1/2)  JDBC URL for a database  Identifies the database to be connected  Consists of three-part: jdbc:<subprotocol>:<subname> Protocol: JDBC is the only protocol in JDBC Protocol: JDBC is the only protocol in JDBC Subname: indicates the location and name of the database to be accessed. Syntax is driver specific Subname: indicates the location and name of the database to be accessed. Syntax is driver specific Sub-protocol: identifies a database driver Sub-protocol: identifies a database driver Ex) jdbc:mysql://oopsla.snu.ac.kr/mydb The syntax for the name of the database is a little messy and is unfortunately vendor specific
  • 11. JDBC URL Vendor of database, Location of database server and name of database Username Password Step 2 : Connecting to a Database (2/2)  The DriverManager allows you to connect to a database using the specified JDBC driver, database location, database name, username and password.  It returns a Connection object which can then be used to communicate with the database. Connection connection = DriverManager.getConnection("jdbc:mysql://oopsla.snu.ac.kr/mydb",“useri d",“password"); JDBC URL Vendor of database, Location of database server and name of database Username Password
  • 12. Step 3 : Executing SQL (1/2)  Statement object  Can be obtained from a Connection object  Sends SQL to the database to be executed  Statement has three methods to execute a SQL statement:  executeQuery() for QUERY statements  Returns a ResultSet which contains the query results  executeUpdate() for INSERT, UPDATE, DELETE statements  Returns an integer, the number of affected rows from the SQL  execute() for either type of statement Statement statement = connection.createStatement();
  • 13. Statement stmt = conn.createStatement(); ResultSet rset = stmt.executeQuery ("select RENTAL_ID, STATUS from ACME_RENTALS"); Statement stmt = conn.createStatement(); int rowcount = stmt.executeUpdate ("delete from ACME_RENTAL_ITEMS where rental_id = 1011"); Step 3 : Executing SQL (2/2)  Execute a select statement  Execute a delete statement
  • 14. Step 4 : Processing the Results (1/2)  JDBC returns the results of a query in a ResultSet object  ResultSet object contains all of the rows which satisfied the conditions in an SQL statement  A ResultSet object maintains a cursor pointing to its current row of data  Use next() to step through the result set row by row  next() returns TRUE if there are still remaining records  getString(), getInt(), and getXXX() assign each value to a Java variable Record 1 Record 2 Record 3 Record 4 ResultSetInternal Pointer The internal pointer starts one before the first record
  • 15. Step 4 : Processing the Results (2/2)  Example Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(“SELECT ID, name, score FROM table1”); While (rs.next()){ int id = rs.getInt(“ID”); String name = rs.getString(“name”); float score = rs.getFloat(“score”); System.out.println(“ID=” + id + “ ” + name + “ ” + score);} NOTE You must step the cursor to the first record before read the results This code will not skip the first record ID name score 1 James 90.5 2 Smith 45.7 3 Donald 80.2 Table1 Output ID=1 James 90.5 ID=2 Smith 45.7 ID=3 Donald 80.2
  • 16. Step 5 : Closing Database Connection  It is a good idea to close the Statement and Connection objects when you have finished with them  Close the ResultSet object rs.close();  Close the Statement object stmt.close();  Close the connection connection.close();
  • 17. The PreparedStatement Object  A PreparedStatement object holds precompiled SQL statements  Use this object for statements you want to execute more than once  A PreparedStatement can contain variables (?) that you supply each time you execute the statement // Create the prepared statement PreparedStatement pstmt = con.prepareStatement(“ UPDATE table1 SET status = ? WHERE id =?”) // Supply values for the variables pstmt.setString (1, “out”); pstmt.setInt(2, id); // Execute the statement pstmt.executeUpdate();

Editor's Notes

  • #14: Dynamically Executing an Unknown SQL Statement The following example uses execute() to dynamically execute an unknown statement: public void executeStmt (String statement) throws SQLException { Statement stmt = conn.createStatement(); // Execute the statement boolean result = stmt.execute(statement); if (result) {// statement was a query ResultSet rset = stmt.getResultSet(); // Process the results ... } else {// statement was an update or DDL int updateCount = stmt.getUpdateCount(); // Process the results ... }}