Java Reference
In-Depth Information
private
String userid =
new
String("xxx");
private
String pw =
new
String("xxxx");
Connection con =
null
;
ResultSet rs =
null
;
Statement stmt =
null
;
int
returnValue;
public
DBAccess() {
try
{
Class.forName(driver);
}
catch
(ClassNotFoundException e) {
System.out.println("Driver class not found: " +
e);
}
try
{
con =
DriverManager.getConnection(url, userid, pw);
stmt = con.createStatement();
}
catch
(SQLException e) {
System.out.println("No connection: " + e);
}
}
}
6.
In Employee, add the following in the class header:
extends
DBAccess
7.
Add the following import statement:
import
java.sql.*;
8.
Add the following to define a class ResultSet variable:
private
ResultSet rs;
9.
In Employee, add the following code to create a new method called doSelect
private void
doSelect(String select) {
try
{
rs = stmt.executeQuery(select);
}
catch
(SQLException e) {
System.out.println("Problem with " + select + e);
}
}
This method will be the “utility” method that executes all possible selections in the Employee class. Clients will
access other public methods (like getEmpNums) to retrieve Employee information. These public methods will create
the correct SQL
select
statements and invoke doSelect. Because there is no reason for clients to use doSelect (or even
know that it exists), doSelect is “hidden” by being defined as
private
. Notice also that the results are assigned to the
class variable rs.