I am learning Java Web Development by developing an e-Commerce web application using JSP and Servlets with JDBC. I was checking some projects in GitHub, GoogleCode etc and I came across some code which I found unusual like declaring select,update,insert methods in an interface like below :
public interface DBDriver {
public void init( DBConnection connection );
public ResultSet selectQuery( String sqlStatement );
public int updateQuery( String sqlStatement );
public ResultSet select( String table, String[] fields, String where );
public ResultSet select( String table, String[] fields, String where, String[] join, String groupBy[], String having, String orderBy[], int start, int limit );
public int insert( String table, HashMap<String, String> fields );
public int update( String table, HashMap<String, String> fields, String where );
public int update( String table, HashMap<String, String> fields, String where, String orderBy[], int start, int limit );
public int delete( String table, String where );
public int delete( String table, String where, String orderBy[], int start, int limit );
public DBConnection getConnection();
}
And implementing these methods in another class for eg: DBDriverSQL.
One of the implemented method is :
public ResultSet select( String table, String[] fields, String where, String[] join, String groupBy[], String having, String orderBy[], int start, int limit ) {
StringBuilder sql = new StringBuilder();
/* Make sure a table is specified */
if( table == null ) {
throw new RuntimeException();
}
sql.append( "SELECT " );
/* Empty field list means we'll select all fields */
if( fields == null || fields.length < 1 ) {
sql.append( "*" );
}
else {
sql.append( Util.joinArray( fields, "," ) );
}
/* Add table and fields list to query */
sql.append( " FROM " ).append( getFullTableName( table ) );
/* Any JOINs?*/
if( join != null && join.length > 0 ) {
sql.append( " " ).append( Util.joinArray( join, " " ) );
}
/* Searching on a WHERE condition? */
if( where != null && !where.isEmpty() ) {
sql.append( " WHERE " ).append( where );
}
/* Add GROUP BY clause */
if( groupBy != null && groupBy.length > 0 ) {
sql.append( Util.joinArray( groupBy, "," ) );
}
if( having != null && !having.isEmpty() ) {
sql.append( " HAVING " ).append( having );
}
if( orderBy != null && orderBy.length > 0 ) {
sql.append( " ORDER BY " ).append( Util.joinArray( orderBy, "," ) );
}
if( limit > 0 ) {
if( start < 1 ) {
start = 0;
}
sql.append( " LIMIT " ).append( start ).append( "," ).append( limit );
}
/* Return the compiled SQL code */
return selectQuery( sql.toString() );
}
These methods are called in the controller Servlets for data extraction from database. Example:
String where = "listId = " + listId;
String[] fields = { "b.*, l.listId, l.price, l.comment, l.listDate, l.active, l.condition, l.currency, u.*" };
String[] join = { "INNER JOIN bzb.book b ON l.isbn=b.isbn",
"INNER JOIN bzb.user u ON l.userId=u.userId" };
ResultSet result = bzb.getDriver().select( "booklisting l", fields, where, join, null, null, null, 0, 1 );
My question is, whether this method is considered as a good practice compared to standard JDBC procedure like:
String sql = "select SetID,SetName,SetPrice,SetQuality from setdetails where heroID = " + id;
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while (rs.next()) {
lists.add(new Set(rs.getInt("SetID"), rs.getString("SetName"), rs.getString("SetPrice"), rs.getString("SetQuality")));
}
return lists;