Академический Документы
Профессиональный Документы
Культура Документы
JDBC
© 2007 ArisGlobal
Objectives
– Retrieving Rows
• The code snippet to retrieve books written by an author from the titles
table using the PreparedStatement object is:
String str = "SELECT * FROM titles WHERE au_id = ?";
PreparedStatement ps= con.prepareStatement(str);
ps.setString(1, "1001");
ResultSet rs=ps.executeQuery();
– Inserting Rows
• The code snippet to create a PreparedStatement object that inserts a row
into authors table by passing authors data at runtime is:
String str = "INSERT INTO authors (au_id, au_fname, au_lname) VALUES
(?, ?, ?)";
PreparedStatement ps = con.prepareStatement(str);
ps.setString(1, "1001");
ps.setString(2, "Abraham");
ps.setString(3, "White");
int rt=ps.executeUpdate();
Querying and Modifying Data Using the
PreparedStatement Object (Contd.)
– Updating and Deleting Rows
• The code snippet to modify the state to CA where city is Oakland
in the authors table using the PreparedStatement object is:
String str = "UPDATE authors SET state= ? WHERE city= ? ";
PreparedStatement ps = con.prepareStatement(str);
ps.setString(1, "CA");
ps.setString(2, "Oakland");
int rt=ps.executeUpdate();
– The code snippet to delete a row from the authors table
where author’s first name is Abraham using the
PreparedStatement object is:
String str = "DELETE FROM authors WHERE au_fname= ? ";
PreparedStatement ps = con.prepareStatement(str);
ps.setString(1, "Abraham");
int rt=ps.executeUpdate();
Demonstration- Creating an Application that
Uses PreparedStatement Object
Example
import java.sql.*;
import java.util.*;
class Prepare {
public static void main(String args[]) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver"); Connection
con=DriverManager.getConnection("jdbc:oracle:thin:@CHARULB:
1521:orcl","user1","user1");
Statement stmt = con.createStatement();
PreparedStatement ps=con.prepareStatement("UPDATE emp SET
empName =? WHERE empID =? ");
Example
Continued…
ps.setString(1, "Tom");
ps.setInt(2,3);
ps.executeUpdate();
ResultSet rs = stmt.executeQuery("SELECT * FROM emp");
while(rs.next()){
String s=rs.getString("empName");
int n=rs.getInt("empID");
System.out.println("Employee Name = " + s);
System.out.println("Employee Id = " + n);
} con.close();
Example
Continued…
//Here 1 and 2 are the sequential number of values to be
set.
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
Managing Database Transactions
– A transaction:
• Is a set of one or more SQL statements that are executed as a
single unit.
• Is complete only when all the SQL statements in a transaction
execute successfully.
• Maintains consistency of data in a database.
Managing Database Transactions
(Contd.)
New D/B
On Commit
SQL Query
Database to update the
table
Old D/B
On Rollback
Managing Database Transactions
(Contd.)
– A batch:
• Is a group of update statements that are sent to a database to be
executed as a single unit.
• Reduces network calls between the application and the database.
• Is a more efficient way as compared to the processing of a single
SQL statement.
Performing Batch Updates (Contd..)
– Implementing Batch Updates in JDBC
• The Statement or PreparedStatement interface provides the
following methods to create and execute a batch of SQL
statements:
– void addBatch(): Adds an SQL statement to a batch.
– int executeBatch(): Sends a batch of SQL statements to a database
for processing and returns the total number of the rows updated.
– void clearBatch(): Removes the SQL statements from the batch.
Method Description
Method Description
Method Description
int getColumnType(int Retrieves the SQL data type of the table column
column_index) corresponding to the index passed as a parameter.
Using Metadata in JDBC (Contd.)
Method Description
Method Description
class ConnectMeta {
public static void main(String args[]) {
try{
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection
connection=DriverManager.getConnection("jdbc:oracle:thi
n:@CHARULB:1521:orcl","user1","user1");
DatabaseMetaData meta=connection.getMetaData();
Example
Continued…
System.out.print("Database : "+meta.getDatabaseProductName());
System.out.println("Database Product Version
:"+meta.getDatabaseProductVersion());
System.out.println("Driver name : "+meta.getDriverName());
System.out.println("Driver version : "+meta.getDriverVersion());
System.out.println("Max Column Length :
"+meta.getMaxColumnNameLength());
System.out.println("Max Row Size : "+meta.getMaxRowSize());
connection.close();
}catch(Exception ex){
System.out.println(ex);
System.exit(0);
}
}
}
Summary