I wrote some code for Oracle that worked great. I was then asked to support the embedded Derby database using the same code. One inconsistent behavior I noticed is the Oracle driver returns a String object from a query while the Derby driver returns Character object. In a regular JDBC program, Derby returns a String object, which indicates the problem lies with Hibernate.
See the code below. Are there any work-arounds for this? I don't want to conditionally handle each database. Thanks!!!
/
Code:
********** SNIPPET FROM MY HIBERNATE PROJECT *************/
String sSQL = "select ft_name as NAME, 'ft' as AOTYPE from faulttree " +
"union " +
"select dt_name as NAME, 'dt' as AOTYPE from decisiontree " +
"union " +
"select exp_name as NAME, 'expr' as AOTYPE from expression " +
"union " +
"select dt_type_name as NAME, 'dt_type' as AOTYPE from decisiontreetype";
AOType aoType = null;
List list = session.createSQLQuery(sSQL).list();
aoTypes = new HashMap(list.size());
for ( Iterator iter = list.iterator(); iter.hasNext(); ) {
Object[] obj = (Object[])iter.next();
// obj[0] is a String object. Fine and dandy
//----------------------- The problem is here! -------------------------
// obj[1] is a String object using Oracle, but a Character object in
// Derby. Not good!
//--------------------------------------------------------------------------
System.out.println(obj[0] + " <<>> " + obj[1]);
}
/******************* JDBC TEST *********************/
package calc;
import java.sql.*;
public class JDBCTest {
Connection conn;
public static void main(String[] args) {
new JDBCTest();
}
public JDBCTest() {
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
String url = "jdbc:derby:MyTestDB;create=true";
conn = DriverManager.getConnection(url, "", "");
doTest();
conn.close();
}
catch (ClassNotFoundException ex) {System.err.println(ex.getMessage());}
catch (IllegalAccessException ex) {System.err.println(ex.getMessage());}
catch (InstantiationException ex) {System.err.println(ex.getMessage());}
catch (SQLException ex) {System.err.println(ex.getMessage());}
}
private void doTest() {
System.out.println("TESTING SELECT");
String sSQL = "select ft_name as NAME, 'ft' as AOTYPE from faulttree " +
"union " +
"select dt_name as NAME, 'dt' as AOTYPE from decisiontree " +
"union " +
"select exp_name as NAME, 'expr' as AOTYPE from expression " +
"union " +
"select dt_type_name as NAME, 'dt_type' as AOTYPE from decisiontreetype";
try {
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(sSQL);
while (rs.next()) {
String s1 = rs.getString("NAME");
String s2 = rs.getString("AOTYPE"); // <------- This works just fine!
System.out.println(s1 + " <<>> " + s2);
}
}
catch (SQLException ex) {
System.err.println(ex.getMessage());
}
}
}