SCD Lec 15 PDF
SCD Lec 15 PDF
JDBC 1
Useful Statement Methods (N-I)
executeQuery
JDBC 2
Example Code 15.1
Executing SQL DML Statements
/* This progarm will take two command line argument
that are used to update records in the database */
import java.sql.*; //step 1
public class JdbcDmlEx {
public static void main (String args [ ]){
try {
//steps 2 to 5
Class.forName(“driver name”);
Connection con=null;
con = DriverManager.getConnection(url, usr, pwd);
Statement st = con.createStatement();
JDBC 3
Example Code 15.1
Executing SQL DML Statements (cont.)
//Step 6: Execute the Query / DML
String addVar = args[0];
String nameVar = args[1];
String sql = “UPDATE Person ” +
“ SET address = ‘ “ + addVar + ” ’ ” +
“ WHERE name = ‘ “ + nameVar + ” ’ ” ;
int num = st.executeUpdate(sql);
JDBC 4
Example Code 15.1
Executing SQL DML Statements (cont.)
//Step 8: close the connection
con.close();
}catch (Exception sqlEx) {
System.out.println(sqlEx);
}
} //end main
}//end class
JDBC 5
Compile & Execute
Before execution
After execution
6 JDBC
Useful Statement Methods
(Continued)
getMaxRows( ) / setMaxRows(int)
◦ Determines the number of rows a ResultSet
may contain
◦ Unless explicitly set, the number of rows are
unlimited (return value of 0)
getQueryTimeout( ) / setQueryTimeout(int)
◦ Specifies the amount of a time (seconds) a driver
will wait for a STATEMENT to complete before
throwing a SQLException
JDBC 7
Different Types of Statements
Overview
◦ Through the Statement object, SQL statements
are sent to the database.
◦ Three types of statement objects are available:
1. Statement
for executing a simple SQL statements
2. PreparedStatement
for executing a precompiled SQL statement passing in
parameters
3. CallableStatement
for executing a database stored procedure
JDBC 8
Prepared Statements
JDBC 9
Prepared Statements (Precompiled
Queries)
Idea
◦ If you are going to execute similar SQL
statements multiple times, using “prepared”
(parameterized) statements can be more efficient
JDBC 10
Prepared Statement, Example
PreparedStatement pStmt =
con.prepareStatement("UPDATE tableName " +
“SET columnName = ? " +
“WHERE columnName =
?");
pStmt.setString(1, stringValue);
pStmt.setInt (2, intValue);
pStmt.executeUpdate();
JDBC 11
Example Code
JDBC 12
Example Code: Modify JdbcDmlEx.java 15.1
Executing Prepared Statements
/* Modification to the last example code 15.1,
to show the usage of prepared statements */
import java.sql.*; // step1
public class JdbcDmlEx {
public static void main (String args [ ]){
try {
//steps 2 to 4
Class.forName(“driver name”);
Connection con=null;
con = DriverManager.getConnection(url, usr, pwd);
JDBC 13
Example Code: Modify JdbcDmlEx.java 15.1
Executing Prepared Statements
//Step 5: Create the statement
PreparedStatement pStmt = null;
pStmt.setString(1 , addVar);
pStmt.setString(2, nameVar);
// sql = “UPDATE Person SET address = “defence” WHERE name = “ali” ”
JDBC 14
Example Code: Modify JdbcDmlEx.java 15.1
Executing Prepared Statements
} //end main
}//end class
JDBC 15
Compile & Execute
Before execution
After execution
16 JDBC
Task
Perform Insert/update/Delete operations
by designing following Screen:
JDBC 17
Result Set
JDBC 18
ResultSet
Row numbers
ResultSet
0 id Name Address phoneNum
JDBC 19