filename:::: lookerUpper.java
package myna.utils;
import java.sql.*;
// communicate with database
import myna.utils.*;
// Logger saves administrative info to file
// MiscDB utility functions
public class LookerUpper {
Connection dbConn=null;
PreparedStatement dbPrepStmt=null;
String theQueryString=null;
// "SELECT then
UMBER,THEADDR FROM PHONEBOOK WHERE then
AME=?;";
String driverName=null;
// "sun.jdbc.odbc.JdbcOdbcDriver";
String dbUrl=null;
// ="jdbc
dbc:IQTEST";
String theUser="usr";
String thePwd="pwd";
Logger lg;
public LookerUpper(String drvrName, String dbName,String qString,Logger L) throws SQLException
{
driverName=drvrName;
try
{
Class.forName(driverName);
}
catch(Exception E)
{
E.printStackTrace();
L.logIt("LookerUpper failed with "+E);
return;
}
dbUrl=dbName;
theQueryString=qString;
lg=L;
dbConn=DriverManager.getConnection(dbUrl,theUser,thePwd);
dbPrepStmt=dbConn.prepareStatement(theQueryString);
lg.logIt("LookerUpper connected to "+dbUrl);
}
public void close(){ // ignore exceptions, try to close up.
lg.logIt("LookerUpper.close()");
try
{
if(dbPrepStmt!=null)
dbPrepStmt.close();
}
catch(java.lang.Exception E)
{
lg.logIt("error in close: ",E);
}
finally{lg.logIt("finalize: closing?");
try
{
if(dbConn!=null)dbConn.close();
lg.logIt("finalize: closed");
}
catch(java.lang.Exception E)
{
lg.logIt("error in close: ",E);
}
}
}
public ResultSet lookup(String V){ // lookup value V;
specifically
lg.logIt("looking up: "+V+";
with query="+theQueryString);
try
{ // initialization of dbPrepStmt in init.
dbPrepStmt.setString(1,V);
if(!dbPrepStmt.execute())
{
lg.logIt("no result set");
return null;}
return dbPrepStmt.getResultSet();
}
catch (java.lang.Exception ex)
{
/* ex.printStackTrace();
*/
lg.logIt("lookup: "+ex);
return null;
}
}
}