import java.sql.*; import java.lang.reflect.Method; import java.lang.reflect.Modifier; public class DerbyFloat { /* Derby */ static final String userName = "APP"; static final String password = "APP"; static final String connectionURL = // "jdbc:derby://localhost:1527/testdb;retrieveMessagesFromServerOnGetMessage=true;create=true;"; "jdbc:derby://localhost:1527/sun-appserv-samples;retrieveMessagesFromServerOnGetMessage=true;"; static final String driverName = "org.apache.derby.jdbc.ClientDriver"; Connection conn; public DerbyFloat() {} void init() throws SQLException { conn = getConnection(driverName, connectionURL, userName, password); // printDataBaseMetaData(conn); } private static Connection getConnection(String driverName, String connectionURL, String userName, String password) throws SQLException { Connection conn = null; try { Class.forName (driverName); } catch (ClassNotFoundException e) { System.out.println("Could not load the driver class. Error is " + e); } try { conn = DriverManager.getConnection(connectionURL, userName, password); conn.setAutoCommit(false); } catch (SQLException e) { System.out.println("Error while getting connection"); SQLException currentException = e; do { System.out.println("Exception is" + currentException); System.out.println( "getMessage()" + currentException.getMessage()); System.out.println( "getErrorCode()" + currentException.getErrorCode()); System.out.println( "getSQLState()" + currentException.getSQLState()); currentException = currentException.getNextException(); } while (currentException != null); throw e; } return conn; } static void printDataBaseMetaData(Connection con) throws SQLException { DatabaseMetaData metaData = con.getMetaData(); Method[] declaredMethods = DatabaseMetaData.class.getDeclaredMethods(); for(int i = 0 ; i < declaredMethods.length; i++) { Method currentMethod = declaredMethods[i]; if(currentMethod.getParameterTypes().length == 0) { //Trace only methods that require no iput System.out.print(currentMethod.getName() + "():"); try { System.out.println(currentMethod.invoke(metaData,(Object[]) null) ); } catch (Throwable e) { System.out.println("Exception for " + Modifier.toString(currentMethod.getModifiers() ) + currentMethod.getName() + e); } } } System.out.print("supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE): " + metaData.supportsTransactionIsolationLevel(Connection.TRANSACTION_SERIALIZABLE)); } public void insertRows() throws java.sql.SQLException { PreparedStatement ps; try { ps = conn.prepareStatement("DROP TABLE DERBYFLOAT"); ps.executeUpdate(); } catch (SQLException e) { System.out.println("Table does not exist"); } ps = conn.prepareStatement("CREATE TABLE DERBYFLOAT (ID INT PRIMARY KEY, FLOATDATA FLOAT)"); // ps = conn.prepareStatement("CREATE TABLE DERBYFLOAT (ID INT PRIMARY KEY, FLOATDATA FLOAT(24))"); ps.executeUpdate(); ps = conn.prepareStatement("DELETE FROM DERBYFLOAT"); ps.executeUpdate(); ps = conn.prepareStatement( "INSERT INTO DERBYFLOAT(ID, FLOATDATA) VALUES(1, 1)"); ps.executeUpdate(); ps = conn.prepareStatement( "INSERT INTO DERBYFLOAT(ID, FLOATDATA) VALUES(2, 124567890123456)"); ps.executeUpdate(); ps = conn.prepareStatement( "INSERT INTO DERBYFLOAT(ID, FLOATDATA) VALUES(3, 3.4028235E37)"); ps.executeUpdate(); ps = conn.prepareStatement( "INSERT INTO DERBYFLOAT(ID, FLOATDATA) VALUES(4, 3.4028235E38)"); ps.executeUpdate(); } public void testFloat() throws java.sql.SQLException { PreparedStatement ps = conn.prepareStatement( "SELECT ID, FLOATDATA FROM DERBYFLOAT"); ResultSet rs = ps.executeQuery(); ResultSetMetaData rsmd = rs.getMetaData(); while(rs.next()) { // for(int i=1;i<= rsmd.getColumnCount();i++) { /* Object o = rs.getObject(i); String columnTypeName = rsmd.getColumnTypeName(i); System.out.println("column " + (i) + " type: " + columnTypeName + " (" + rsmd.getColumnType(i) + ") " + "\t\tJava Type: " + o.getClass()); */ System.out.println("\n Value of field 1 uing getInt() : " + rs.getInt(1)); try { System.out.println("\n Value of field 2 using getFloat() : " + rs.getFloat(2)); } catch (SQLException e) { System.out.println("\n Value of field 2 using getFloat() resulted in a SQLException"); e.printStackTrace(); System.out.println("\n Value of field 2 using getObject() : " + rs.getObject(2)); } // } } } public static final void main (String args []) { DerbyFloat dbFloat = new DerbyFloat(); try { dbFloat.init(); dbFloat.insertRows(); dbFloat.testFloat(); } catch (SQLException ex) { System.out.println("SQLException : "+ ex); ex.printStackTrace(); } } }