db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r498842 - /db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/util.java
Date Mon, 22 Jan 2007 23:04:45 GMT
Author: djd
Date: Mon Jan 22 15:04:41 2007
New Revision: 498842

URL: http://svn.apache.org/viewvc?view=rev&rev=498842
Log:
DERBY-2228 Fix ij's util class to make correct determination if running on J2SE or JSR 169.
Also set DECIMAL parameter in PreparedStatement using setString and retry using setLong if
a conversion error occurs. This fixes the NullPointerExcecption seen in DEBRY-680. Previously
the code was using the ResultSetMetaData which was incorrect, as it has no relationship
to the types of the parameters.

Modified:
    db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/util.java

Modified: db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/util.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/util.java?view=diff&rev=498842&r1=498841&r2=498842
==============================================================================
--- db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/util.java (original)
+++ db/derby/code/trunk/java/tools/org/apache/derby/impl/tools/ij/util.java Mon Jan 22 15:04:41
2007
@@ -54,17 +54,21 @@
  */
 public final class util implements java.security.PrivilegedAction {
 	
-	private static boolean HAVE_BIG_DECIMAL;
+	private static boolean IS_AT_LEAST_JDBC2;
 	
 	{
-		boolean haveBigDecimal;
+		boolean isAtLeastJDBC2;
 		try {
-			Class.forName("java.math.BigDecimal");
-			haveBigDecimal = true;
+            // Need to test to see if this is
+            // currently JDBC 2 or JSR169.
+            // Checking for BigDecimal doesn't work because
+            // BigDecimal exists in J2ME/CDC/Foundation 1.1
+            Class.forName("java.sql.Driver");
+			isAtLeastJDBC2 = true;
 		} catch (Throwable t) {
-			haveBigDecimal = false;
+			isAtLeastJDBC2 = false;
 		}
-		HAVE_BIG_DECIMAL = haveBigDecimal;
+		IS_AT_LEAST_JDBC2 = isAtLeastJDBC2;
 	}
 	
 	private static final Class[] DS_GET_CONN_TYPES = {"".getClass(), "".getClass()};
@@ -647,7 +651,7 @@
 				
 				if (sqlType == Types.DECIMAL)
 				{
-					if (util.HAVE_BIG_DECIMAL)
+					if (util.IS_AT_LEAST_JDBC2)
 					{
 						ps.setObject(c,rs.getObject(c),
 								 sqlType,
@@ -660,21 +664,21 @@
 						// pass values around, but for integral types
 						// first convert to a integral type from the DECIMAL
 						// because strings like 3.4 are not convertible to
-						// an integral type.
-						switch (ps.getMetaData().getColumnType(c))
-						{
-						case Types.BIGINT:
-							ps.setLong(c, rs.getLong(c));
-						    break;
-						case Types.INTEGER:
-						case Types.SMALLINT:
-						case Types.TINYINT:
-							ps.setInt(c, rs.getInt(c));
-							break;
-						default:
-							ps.setString(c,rs.getString(c));
-						    break;
-						}								
+						// an integral type. Of course in JSR169 we have
+                        // no way to determine the parameter types,
+                        // ParameterMetaData is not supported.
+                        // So convert as string, and on a conversion error
+                        // try as a long.
+                        
+                        try {
+                            ps.setString(c, rs.getString(c));
+                        } catch (SQLException e) {
+                            // 22018 - invalid format
+                            if ("22018".equals(e.getSQLState()))
+                                ps.setLong(c, rs.getLong(c));
+                            else
+                                throw e;
+                        }						
 					}
 					
 				}



Mime
View raw message