Jump to content
Wikibooks The Free Textbook Project

Java JDBC using SQLite/Example base class

From Wikibooks, open books for an open world

This page provides an example of a base class you can use as a skeleton for creating your own, and ideas of ways of extending this class. You should note that your central focus of concern should be on getting your initial context set up correctly, and there are many and diverse ways of skinning this particular cat. This base class is abstract since we will want to extend it to more precisely suit our requirements. There is however nothing particularly esoteric in there to prevent you concretizing this class by declaring it public and using it out of the box, modifying and supplying your own constructor(s) accordingly.

You should also note that there are a number of fields and methods in this base class which are declared public which more properly should be declared private, however these have been left public for visibility and since you may wish to experiment with the implications of some of the calls. Additionally, leaving these fields and methods public gives you a broad brush scope to invoke and adjust them by reflection.

Base Class

[edit | edit source ]
importjava.sql.Connection;
importjava.sql.DriverManager;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
/** Database connection class & utilities **/
abstractclass Db{
publicStringsDriver="";
publicStringsUrl=null;
publicintiTimeout=30;
publicConnectionconn=null;
publicStatementstatement=null;
/* Stub constructor for quick instantiation o/t fly for using some of the ancillary stuff */
publicDb()
{}
/* quick and dirty constructor to test the database passing the DriverManager name and the fully loaded url to handle */
/* NB this will typically be available if you make this class concrete and not abstract */
publicDb(StringsDriverToLoad,StringsUrlToLoad)throwsException
{
init(sDriverToLoad,sUrlToLoad);
}

publicvoidinit(StringsDriverVar,StringsUrlVar)throwsException
{
setDriver(sDriverVar);
setUrl(sUrlVar);
setConnection();
setStatement();
}
privatevoidsetDriver(StringsDriverVar)
{
sDriver=sDriverVar;
}
privatevoidsetUrl(StringsUrlVar)
{
sUrl=sUrlVar;
}
publicvoidsetConnection()throwsException{
Class.forName(sDriver);
conn=DriverManager.getConnection(sUrl);
}
publicConnectiongetConnection(){
returnconn;
}
publicvoidsetStatement()throwsException{
if(conn==null){
setConnection();
}
statement=conn.createStatement();
statement.setQueryTimeout(iTimeout);// set timeout to 30 sec.
}
publicStatementgetStatement(){
returnstatement;
}
publicvoidexecuteStmt(Stringinstruction)throwsSQLException{
statement.executeUpdate(instruction);
}
// processes an array of instructions e.g. a set of SQL command strings passed from a file
//NB you should ensure you either handle empty lines in files by either removing them or parsing them out 
// since they will generate spurious SQLExceptions when they are encountered during the iteration....
publicvoidexecuteStmt(String[]instructionSet)throwsSQLException{
for(inti=0;i<instructionSet.length;i++){
executeStmt(instructionSet[i]);
}
}
publicResultSetexecuteQry(Stringinstruction)throwsSQLException{
returnstatement.executeQuery(instruction);
}
publicvoidcloseConnection(){
try{conn.close();}catch(Exceptionignore){}
}
}

AltStyle によって変換されたページ (->オリジナル) /