FIX: NullPointerException when you set the Timestamp parameter to Null with JDBC (827223)



The information in this article applies to:

  • Microsoft SQL Server 2000 Driver for JDBC

SYMPTOMS

When you use the JDBC setTimestamp method, the setTime method, or the setDate method to set a parameter value to Null, you may receive a java.lang.NullPointerException exception.

RESOLUTION

Install Microsoft SQL Server 2000 Driver for JDBC Service Pack 2 (SP2).

WORKAROUND

Use the setNull method to set a parameter to Null. See the comments in the code sample in this article for more information.

STATUS

Microsoft has confirmed that this is a bug in Microsoft SQL Server 2000 Driver for JDBC version 2.2.0033. This bug was corrected in Microsoft SQL Server 2000 Driver for JDBC Service Pack 2. Note This bug does not occur with earlier versions of the driver, including Service Pack 1 (SP1). Version 2.2.003 was an interim release that was only available by contacting Microsoft Product Support Services. For additional information about the availability of version 2.2.0033 of the driver, click the following article number to view the article in the Microsoft Knowledge Base:

818504 FIX: JDBC Driver Overwrites Timestamp Parameter Values with the Last Value Set

MORE INFORMATION

Steps to reproduce the behavior

  1. Use the following SQL code to create a table in your SQL Server database:
    CREATE TABLE kbTestTable (col1 datetime, col2 varchar(10))
  2. Compile and run the following Java code to reproduce the problem:
    import java.sql.*;
    
    public class Class1
    {
    	public static void main (String[] args)
    	{
    		try
    		{
    			Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver"); 
    			Connection con = DriverManager.getConnection("jdbc:microsoft:sqlserver://servername:1433","uid","pwd"); 
    	
    			PreparedStatement ps = con.prepareStatement("INSERT INTO kbTestTable VALUES (?, 'newvalue')");
    			ps.setTimestamp(1, null);			
    			//ps.setNull(1, java.sql.Types.TIMESTAMP); //use this as a workaround
    			
    			ps.executeUpdate();			
    			System.out.println("INSERT succeeded");
    			con.close();
    		}
    		catch (Exception ex)
    		{
    			ex.printStackTrace();
    		}
    	}
    }
    The following exception and call stack occurs:
    java.lang.NullPointerException
            at com.microsoft.jdbc.base.BasePreparedStatement.setTimestamp(Unknown Source)
            at Class1.main(Class1.java:13)

Modification Type:MajorLast Reviewed:12/11/2003
Keywords:kbfix kbQFE KB827223 kbAudDeveloper