Microsoft KB Archive/894557

From BetaArchive Wiki
Knowledge Base


Article ID: 894557

Article Last Modified on 10/7/2005



APPLIES TO

  • Microsoft SQL Server 2000 Driver for JDBC




SYMPTOMS

When you insert an out-of-range datetime value in a Microsoft SQL Server-based server by using the Microsoft SQL Server 2000 Driver for JDBC, the server may receive the following error message and end the client connection:

2004-08-12 15:56:47.77 spid51 Error: 17805, Severity: 20, State: 3
2004-08-12 15:56:47.77 spid51 Invalid buffer received from client.

For example, the date "March 15, 2002" may be represented as "2/03/15". This date will be interpreted as out-of-range according to the "yyyy/MM/dd" pattern. When the out-of-range error is detected by the JDBC driver, the client application may receive an exception that is similar to the following:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]The year, 2, is outside the range allowed by the SQL Server.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSDateTime.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCParameter.write(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.submitRequest(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.execute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
at Test.main(Test.java:40)

Subsequent tries to use the same connection may cause the following exception:

java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Connection reset
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
at com.microsoft.jdbc.base.BaseStatement.executeQueryInternal(Unknown Source)
at com.microsoft.jdbc.base.BasePreparedStatement.executeQuery(Unknown Source)
at Test.main(Test.java:58)

RESOLUTION

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next SQL Server 2000 Driver for JDBC service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:

Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question.

The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in Coordinated Universal Time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.

  Date        Time   Version   Size     File name
  -----------------------------------------------------
  1-Oct-2004  12:04  2.2.0043  286,953  Msbase.jar
  1-Oct-2004  12:04  2.2.0043   67,167  Mssqlserver.jar
  1-Oct-2004  12:04  2.2.0043   59,072  Msutil.jar

MORE INFORMATION

To work around the problem if the specified date indicates the year with a format of 19yy or 20yy, format the date in the client code as follows:

String dateString = "2/03/15"
// Add "0" if the second character is "/".
String tmpString = dateString.substring(1,2);
if(tmpString.equals("/"))
{
tmpString = "0"
tmpString = tmpString.concat(dateString);
dateString = tmpString;
}

According to the JDBC specification, if you use a four-character year pattern "yyyy" will interpret the year value literally. For example, the year value will be interpreted as 2 instead of 2002. Therefore, you must also use a two-character year pattern for the year. For more information, see the following code:

try
{
// Change the Year figures from 4(yyyy) to 2(yy)
//DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
DateFormat formatter = new SimpleDateFormat("yy/MM/dd");

// remaining code
}

To see this documentation, visit the following Web site:

Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

Steps to reproduce the behavior

  1. Create a table by using the following script:

    USE pubs
    GO
    CREATE TABLE t1(test_varchar VARCHAR(255), test_date DATETIME)
    GO
  2. Compile and then run the following Java code:

    import java.io.*;
    import java.sql.*;
    import java.text.*;
    import java.util.*;
    
    public class Test 
    {
    public static void main(String[] args)
    {
    PreparedStatement pstmt = null;
    Connection connection = null;
    java.sql.Date targetDate;
    String dateString = "2/03/15" // March 15, 2002
    
    try
    {
    Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver" );
    connection = DriverManager.getConnection("jdbc:microsoft:sqlserver://<Server>:1433;databasename=pubs;", "<UserId>", "<PassWd>");
    
    try 
    {
    DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
    //DateFormat formatter = new SimpleDateFormat("yy/MM/dd");
    formatter.setLenient(false);
    targetDate = new java.sql.Date(formatter.parse(dateString).getTime());
    System.out.println("parsed without exception");
    } 
    catch (ParseException e) 
    {
    e.printStackTrace();
    return;
    }
    
    try 
    {
    System.out.println("before INSERT");
    pstmt = connection.prepareStatement("INSERT INTO t1 VALUES (?,?)");
    pstmt.setString(1, "test");
    pstmt.setDate(2, targetDate);
    pstmt.executeUpdate();
    pstmt.close();
    } 
    catch (Exception e) 
    {
    e.printStackTrace();
    } 
    finally 
    {
    if (pstmt != null)
    pstmt.close();
    }
    
    // Execute another query by using the same connection.
    try 
    {
    System.out.println("before SELECT 0");
    pstmt = connection.prepareStatement("SELECT 0");
    ResultSet rs = pstmt.executeQuery();
    } 
    catch (Exception e)
    {
    e.printStackTrace();
    }
    finally 
    {
    if (pstmt != null)
    pstmt.close();
    }
    
    connection.close();
    System.out.println("Finished.");
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }
    }
    }

    Note In this code, replace the <Server>, <UserID>, and <Password> with the name of your computer that is running SQL Server, your user ID, and your password.


REFERENCES

For more information about JDBC, click the following article number to view the article in the Microsoft Knowledge Base:

313100 How to get started with Microsoft JDBC


For more information, click the following article number to view the article in the Microsoft Knowledge Base:

824684 Description of the standard terminology that is used to describe Microsoft software updates


Keywords: kberrmsg kbbug kbfix kbqfe kbdatabase kbprogramming kbjdbc KB894557