[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
public class DataSelector{
private Connection conn;
protected PreparedStatement statement1PS;
protected PreparedStatement statement2PS;
protected PreparedStatement statement3PS;
public void DataSelector() {
createDatabaseConnectionAndPrepareStatements();
}
protected Connection createDatabaseConnectionAndPrepareStatement(){
if ((conn == null)) {
Class.forName("drivername").newInstance();
conn=
DriverManager.getConnection(<login info>);
statement1PS = conn.prepareStatement("select * from Table1 where field1 = ?");
statement2PS = conn.prepareStatement("select * from Table2 where field2 = ?");
statement3PS = conn.prepareStatement("select * from Table3 where field3 = ?");
}
}
public void queryData(String value1, String value2, String value3){
createDatabaseConnectionAndPrepareStatements();
statement1PS.setString(1, value1);
ResultSet emailResult = statement1PS.executeQuery();
<do some things with the results>
statement2PS.setString(1, value2);
ResultSet emailResult = statement2PS.executeQuery();
<do some things with the results>
statement3PS.setString(1, value3);
ResultSet emailResult = statement3PS.executeQuery();
<do some things with the results>
}
public void cleanUp() {
if (conn!= null) {
conn.close();
conn = null;
}
statement1PS.close();
statement2PS.close();
statement3PS.close();
}
}
I am not receiving any out of memory exceptions.
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
[OCP 17 book] | [OCP 11 book] | [OCA 8 book] [OCP 8 book] [Practice tests book] [Blog] [JavaRanch FAQ] [How To Ask Questions] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Don't get me started about those stupid light bulbs. |