Professional Documents
Culture Documents
Introduction
JDBC driver types
Steps to Connect to the Database
Transactions and JDBC
Introduction to Java JDBC
3
◦ Java JDBC is a java API to connect and execute query with the database.
◦ JDBC API uses jdbc drivers to connect with the database.
Oracle
Driver
Oracle
Java SQLserver
Application JDBC Driver
SQLServer
Network
MySQL
Driver
MySQL
JDBC Architecture (cont.)
8
S T E P S T O C O N N E C T T O T H E D A T A BA S E
Seven Steps
11
Load the driver class only. The class has a static initialization
block that makes an instance and registers it with the
DriverManager.
try {
Class.forName("com.mysql.jdbc.Driver");
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException cnfe) {
System.out.println("Error loading driver: " + cnfe);
}
3. Open a Connection to a Database
14
Creating a PreparedStatement
A PreparedStatement is associated as a channel with a connection and a compiled SQL
statement. PreparedStatements are also created with a Connection method. The following snippet
shows how to create a parameterized SQL statement with three input parameters:
Idea
– statement.executeQuery("SELECT … FROM …");
• This version returns a ResultSet
– statement.executeUpdate("UPDATE …");
– statement.executeUpdate("INSERT …");
– statement.executeUpdate("DELETE…");
– statement.execute("CREATE TABLE…");
– statement.execute("DROP TABLE …");
But those returns the number of affected rows not resultset
Cont…
24
Example
String query = "SELECT col1, col2, col3 FROM sometable";
ResultSet resultSet = statement.executeQuery(query);
Before closing the connection U should close the objects of any statement.
ResultSet Object
25
• Example
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(“SELECT ID, name, score FROM table1”);
NOTE
While (rs.next()){ You must step the cursor to the first record before
int id = rs.getInt(“ID”); read the results
This code will not skip the first record
String name = rs.getString(“name”);
float score = rs.getFloat(“score”);
System.out.println(“ID=” + id + “ ” + name + “ ” + score);}
Output
ID name score ID=1 James 90.5
1 James 90.5 ID=2 Smith 45.7
2 Smith 45.7 ID=3 Donald 80.2
3 Donald 80.2
Table1
Cont…
30
Assumption
– Query was “SELECT first, last, address FROM…”
• Using column names
while(resultSet.next()) {
System.out.println(
"First name: , last name:, address: \n",
resultSet.getString("first"),
resultSet.getString("last"),
resultSet.getString("address"));
}
Cont…
32
Once the ResultSet and Statement objects have been used, they
must be closed explicitly. This is done by calls to the close() method
of the ResultSet and Statement classes.
rset.close();
sql_stmt.close();
The last step is to close the database connection. This is done by a call to the
close() method of the Connection class.
40
41
Cont…
42
Connection connection =
DriverManager.getConnection(url, userProperties);
connection.setAutoCommit(false);
try {
statement.executeUpdate(...);
statement.executeUpdate(...);
...
connection.commit();
} catch (Exception e) {
try {
connection.rollback();
} catch (SQLException sqle) {
// report problem
}
} finally {
try {
connection.close();
} catch (SQLException sqle) { }
}
Useful Connection Methods (for Transactions)
45
getAutoCommit/setAutoCommit
– By default, a connection is set to auto-commit
– Retrieves or sets the auto-commit mode
• commit
– Force all changes since the last call to commit to become
permanent
– Any database locks currently held by this Connection
object are released
• rollback
– Drops all changes since the previous call to commit
– Releases any database locks held by this Connection
object
Cont…
46
47
Thank you