Databases Reference
In-Depth Information
3.
Create a class ConnectionManagement in the package chapter02 using the
following code and save it in a file named ConnectionManagement.java in the
previously created chapter02 directory:
package chapter02;
import java.sql.*;
public class ConnectionManagement {
private static final String driver =
"oracle.jdbc.driver.OracleDriver";
private static final String connectionString =
"jdbc:oracle:thin:@localhost:1521:TESTDB";
private static final String user = "hr";
private static final String pass = "hr";
private static final int iterations = 100;
public static void singleConnection() throws SQLException {
Connection conn = null;
try {
Class.forName(driver);
conn = DriverManager.getConnection(connectionString,
user, pass);
} catch (Exception e) {
System.out.println(String.format("Error %s",
e.getLocalizedMessage()));
System.exit(1);
}
try {
for (int j = 0; j < iterations; ++j) {
Statement query = conn.createStatement();
ResultSet result = query.executeQuery(
"select first_name,
last_name
from employees");
while (result.next()) {
String name = result.getString("first_name")
+ " "+ result.getString("last_name");
System.out.println(name);
}
query.close();
}
} catch (Exception e) {
System.out.println(String.format("Error %s",
e.getLocalizedMessage()));
System.exit(1);
} finally {
 
Search WWH ::




Custom Search