You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by ma...@apache.org on 2007/10/19 22:26:02 UTC

svn commit: r586596 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/PreparedStatement.java testing/org/apache/derbyTesting/functionTests/tests/derbynet/PrepareStatementTest.java

Author: mamta
Date: Fri Oct 19 13:26:02 2007
New Revision: 586596

URL: http://svn.apache.org/viewvc?rev=586596&view=rev
Log:
DERBY-3046

This commit fixes the null pointer exception by making sure that we do not try to access the object's elements if the object is null. I have also added a test case for this fix. 

Additionally, while working on this, I found that we return different sql state for parameter being out of range for PreparedStatment.setObject and Preparedstatement.setXXX calls in Network Server. I have entered a Jira entry for that issue DERBY-3139.


Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/PrepareStatementTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java?rev=586596&r1=586595&r2=586596&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/PreparedStatement.java Fri Oct 19 13:26:02 2007
@@ -2411,10 +2411,16 @@
 
     void checkForValidParameterIndex(int parameterIndex) throws SqlException {
         if (parameterMetaData_ == null || parameterIndex < 1 || parameterIndex > parameterMetaData_.columns_) {
+        	int totalParameters = 0;
+            if (parameterMetaData_ != null)
+            	//Load totalParmeters with correct number of parameters if 
+            	//ParameterMetaData_ is not null. We will need that in the error
+            	//message.
+            	totalParameters = parameterMetaData_.columns_;
             throw new SqlException(agent_.logWriter_, 
                 new ClientMessageId(SQLState.LANG_INVALID_PARAM_POSITION),
                 new Integer(parameterIndex), 
-                new Integer(parameterMetaData_.columns_));
+                new Integer(totalParameters));
         }
     }
 

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/PrepareStatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/PrepareStatementTest.java?rev=586596&r1=586595&r2=586596&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/PrepareStatementTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/derbynet/PrepareStatementTest.java Fri Oct 19 13:26:02 2007
@@ -1104,6 +1104,57 @@
         st.close();
     }
 
+    /**
+     * A test case for DERBY-3046
+     * We were running into null pointer exception if the parameter count
+     * for PreparedStatement was 0 and the user tried doing setObject
+     * 
+     * @throws Exception
+     */
+    public void testVariationOfSetObject() throws Exception
+    {
+        Statement stmt = createStatement();
+        String createString = "CREATE TABLE WISH_LIST  "
+        	+  "(WISH_ID INT NOT NULL GENERATED ALWAYS AS IDENTITY " 
+        	+  "   CONSTRAINT WISH_PK PRIMARY KEY, " 
+        	+  " ENTRY_DATE TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "
+        	+  " WISH_ITEM VARCHAR(32) NOT NULL) " ;
+        
+        stmt.executeUpdate(createString);
+        PreparedStatement ps = prepareStatement("insert into WISH_LIST(WISH_ITEM) values (?)");
+        //this won't raise any errors because there is one parameter in ps
+        ps.setString(1, "aaa");
+        ps.executeUpdate();
+        
+        //Negative test case. There are no parameter in the following ps
+        ps = prepareStatement("insert into WISH_LIST(WISH_ITEM) values ('bb')");
+        //Try setString when no parameters in ps
+        try {
+        	ps.setString(1, "aaa");
+            fail("Exception expected above!");
+        } catch (SQLException e)  {  
+        	if (usingDerbyNetClient())
+        		//note that SQLState is XCL14. For setObject below, the 
+        		//SQLState is XCL13. I have entered DERBY-3139 for this
+        		//difference in SQLState.
+        		assertSQLState("XCL14", e);
+        	else
+        		assertSQLState("07009", e);
+        }
+        //Try setObject when no parameters in ps
+        try {
+        	ps.setObject(1,"cc",java.sql.Types.VARCHAR); 
+            fail("Exception expected above!");
+        } catch (SQLException e)  {   
+        	if (usingDerbyNetClient())
+        		//note that SQLState is XCL13. For setString above, the 
+        		//SQLState is XCL14. I have entered DERBY-3139 for this
+        		//difference in SQLState.
+        		assertSQLState("XCL13", e);
+        	else
+        		assertSQLState("07009", e);
+        }
+    }
 
     /**
      * Test two different bugs regarding the handling of large