Обсуждение: Re: JDBC exception when call updateNull within an updateable ResultSet

Поиск
Список
Период
Сортировка

Re: JDBC exception when call updateNull within an updateable ResultSet

От
Kris Jurka
Дата:
The following patch allows an updateable result set to set values to null.
Since it is impossible to do hashtable.put(key, null), I have created a
special object to represent the null value.  This is simpler than creating
a separate collection to represent the fields set to null and trying to
keep that in sync with the regular values.

Thanks for the report.

Kris Jurka

On Fri, 11 Oct 2002, Olaf Liepelt wrote:

>
> Hi Kris, I've fund another bug. When creating an updateable result set
> and call 'updateNull' a NullPointerException is thrown: Exception in
> thread "main" java.lang.NullPointerException
>         at java.util.Hashtable.put(Hashtable.java:397)
>         at org.postgresql.jdbc2.AbstractJdbc2ResultSet.updateNull(AbstractJdbc2ResultSet.java:989)
>         at PGTest.main(PGTest.java:50)
>
> code:
>
> import java.sql.*;
>
> public class PGTest {
>     public static void main(String args[])
>         throws Exception
>     {
>         String sql = "create table t(a serial primary key, b int)";
>         Class.forName("org.postgresql.Driver");
>         Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "comrad", "");
>         try
>         {
>             Statement stmt = conn.createStatement();
>             stmt.executeUpdate(sql);
>             sql = "INSERT INTO t (a, b) VALUES (3, 4)";
>             stmt.executeUpdate(sql);
>             stmt.close();
>         } catch (SQLException sqle)
>         {
>             sqle.printStackTrace();
>         }
>
>         try
>         {
>             sql = "select * from t for update";
>             Statement stmt = conn.createStatement();
>             ResultSet rs = stmt.executeQuery(sql);
>             rs.updateNull("b");
>             conn.commit();
>         } catch (SQLException sqle)
>         {
>             sqle.printStackTrace();
>         }
>
>         Statement stmt = conn.createStatement();
>         stmt.executeUpdate("DROP TABLE t");
>         stmt.executeUpdate("DROP sequence t_a_seq");
>         stmt.close();
>         conn.close();
>     }
> }
>
> Thanks Olaf
> --
> =============================
> | Olaf Liepelt              |
> | Sofware Innovations Ltd.  |
> | mail: olafl@comrad.co.nz  |
> =============================
>

Re: JDBC exception when call updateNull within an updateable

От
Barry Lind
Дата:
Kris,

Patch applied.  I reworked it a little if you want to check out my changes.

thanks,
--Barry



Kris Jurka wrote:
> The following patch allows an updateable result set to set values to null.
> Since it is impossible to do hashtable.put(key, null), I have created a
> special object to represent the null value.  This is simpler than creating
> a separate collection to represent the fields set to null and trying to
> keep that in sync with the regular values.
>
> Thanks for the report.
>
> Kris Jurka
>
> On Fri, 11 Oct 2002, Olaf Liepelt wrote:
>
>
>>Hi Kris, I've fund another bug. When creating an updateable result set
>>and call 'updateNull' a NullPointerException is thrown: Exception in
>>thread "main" java.lang.NullPointerException
>>        at java.util.Hashtable.put(Hashtable.java:397)
>>        at org.postgresql.jdbc2.AbstractJdbc2ResultSet.updateNull(AbstractJdbc2ResultSet.java:989)
>>        at PGTest.main(PGTest.java:50)
>>
>>code:
>>
>>import java.sql.*;
>>
>>public class PGTest {
>>    public static void main(String args[])
>>        throws Exception
>>    {
>>        String sql = "create table t(a serial primary key, b int)";
>>        Class.forName("org.postgresql.Driver");
>>        Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/test", "comrad", "");
>>        try
>>        {
>>            Statement stmt = conn.createStatement();
>>            stmt.executeUpdate(sql);
>>            sql = "INSERT INTO t (a, b) VALUES (3, 4)";
>>            stmt.executeUpdate(sql);
>>            stmt.close();
>>        } catch (SQLException sqle)
>>        {
>>            sqle.printStackTrace();
>>        }
>>
>>        try
>>        {
>>            sql = "select * from t for update";
>>            Statement stmt = conn.createStatement();
>>            ResultSet rs = stmt.executeQuery(sql);
>>            rs.updateNull("b");
>>            conn.commit();
>>        } catch (SQLException sqle)
>>        {
>>            sqle.printStackTrace();
>>        }
>>
>>        Statement stmt = conn.createStatement();
>>        stmt.executeUpdate("DROP TABLE t");
>>        stmt.executeUpdate("DROP sequence t_a_seq");
>>        stmt.close();
>>        conn.close();
>>    }
>>}
>>
>>Thanks Olaf
>>--
>>=============================
>>| Olaf Liepelt              |
>>| Sofware Innovations Ltd.  |
>>| mail: olafl@comrad.co.nz  |
>>=============================
>>
>>
>>
>>------------------------------------------------------------------------
>>
>>Index: src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java
>>===================================================================
>>RCS file: /projects/cvsroot/pgsql-server/src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java,v
>>retrieving revision 1.8
>>diff -c -r1.8 AbstractJdbc2ResultSet.java
>>*** src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java    2002/09/11 05:38:45    1.8
>>--- src/interfaces/jdbc/org/postgresql/jdbc2/AbstractJdbc2ResultSet.java    2002/10/14 17:36:51
>>***************
>>*** 623,629 ****
>>              for ( int i = 1; keys.hasMoreElements(); i++)
>>              {
>>                  String key = (String) keys.nextElement();
>>!                 insertStatement.setObject(i, updateValues.get( key ) );
>>              }
>>
>>              insertStatement.executeUpdate();
>>--- 623,633 ----
>>              for ( int i = 1; keys.hasMoreElements(); i++)
>>              {
>>                  String key = (String) keys.nextElement();
>>!                 Object o = updateValues.get(key);
>>!                 if (o instanceof NullObject)
>>!                     insertStatement.setNull(i,java.sql.Types.NULL);
>>!                 else
>>!                     insertStatement.setObject(i, o);
>>              }
>>
>>              insertStatement.executeUpdate();
>>***************
>>*** 774,780 ****
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>--- 778,787 ----
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>***************
>>*** 855,861 ****
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>--- 862,871 ----
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>***************
>>*** 904,910 ****
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>      }
>>
>>
>>--- 914,923 ----
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>      }
>>
>>
>>***************
>>*** 986,992 ****
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), null);
>>
>>
>>      }
>>--- 999,1005 ----
>>          }
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), new NullObject());
>>
>>
>>      }
>>***************
>>*** 1004,1010 ****
>>              Driver.debug("updating object " + fields[columnIndex - 1].getName() + " = " + x);
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>      }
>>
>>
>>--- 1017,1026 ----
>>              Driver.debug("updating object " + fields[columnIndex - 1].getName() + " = " + x);
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>      }
>>
>>
>>***************
>>*** 1152,1158 ****
>>                  Iterator iterator = updateValues.values().iterator();
>>                  for (; iterator.hasNext(); i++)
>>                  {
>>!                     updateStatement.setObject( i + 1, iterator.next() );
>>
>>                  }
>>                  for ( int j = 0; j < numKeys; j++, i++)
>>--- 1168,1178 ----
>>                  Iterator iterator = updateValues.values().iterator();
>>                  for (; iterator.hasNext(); i++)
>>                  {
>>!                     Object o = iterator.next();
>>!                     if (o instanceof NullObject)
>>!                         updateStatement.setNull(i+1,java.sql.Types.NULL);
>>!                     else
>>!                         updateStatement.setObject( i + 1, o );
>>
>>                  }
>>                  for ( int j = 0; j < numKeys; j++, i++)
>>***************
>>*** 1209,1215 ****
>>              Driver.debug("in update String " + fields[columnIndex - 1].getName() + " = " + x);
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>--- 1229,1238 ----
>>              Driver.debug("in update String " + fields[columnIndex - 1].getName() + " = " + x);
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>***************
>>*** 1222,1228 ****
>>
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>--- 1245,1254 ----
>>
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>      }
>>
>>***************
>>*** 1234,1240 ****
>>              Driver.debug("updating Timestamp " + fields[columnIndex - 1].getName() + " = " + x);
>>
>>          doingUpdates = !onInsertRow;
>>!         updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>
>>      }
>>--- 1260,1269 ----
>>              Driver.debug("updating Timestamp " + fields[columnIndex - 1].getName() + " = " + x);
>>
>>          doingUpdates = !onInsertRow;
>>!         if (x == null)
>>!             updateNull(columnIndex);
>>!         else
>>!             updateValues.put( fields[columnIndex - 1].getName(), x );
>>
>>
>>      }
>>***************
>>*** 1585,1587 ****
>>--- 1614,1619 ----
>>
>>  }
>>
>>+ class NullObject {
>>+
>>+ }
>>
>>
>>------------------------------------------------------------------------
>>
>>
>>---------------------------(end of broadcast)---------------------------
>>TIP 2: you can get off all lists at once with the unregister command
>>    (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)
>