Microsoft KB Archive/817419

= FIX: String Output Parameters Truncated to 4000 Characters with JDBC =

Article ID: 817419

Article Last Modified on 2/21/2007

-

APPLIES TO


 * Microsoft SQL Server 2000 Driver for JDBC
 * Microsoft SQL Server for JDBC Service Pack 1

-



SYMPTOMS
String data that is retrieved from varchar output parameters by using the Microsoft SQL Server 2000 Driver for JDBC is truncated to 4000 characters. This problem does not occur with data in ResultSet objects, or in input parameters.



CAUSE
A bug in the driver causes this problem. The problem occurs even if the SendStringParametersAsUnicode connection string property is set to False.



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 service pack that contains this hotfix.

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

http://support.microsoft.com/contactus/?ws=support

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 fix has the file attributes (or later) 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. The Windows version of this fix has the following properties:

Date        Time   Version            Size    File name --  04-Mar-2003  00:51  2.2.0032          286,720  Msbase.jar 04-Mar-2003 00:51  2.2.0032           67,041  Mssqlserver.jar 04-Mar-2003 00:51  2.2.0032           58,903  Msutil.jar

The Unix-based version of this fix has the following properties:

Date        Time   Version            Size    File name --  08-Apr-2003  18:53  2.2.0032          286,720  Msbase.jar 08-Apr-2003 18:53  2.2.0032           67,041  Mssqlserver.jar 08-Apr-2003 18:53  2.2.0032           58,903  Msutil.jar Note Although the file dates are different for each operating system that is listed, internally the files are exactly the same. The best way to determine the version of the driver you are using is to use the getDriverVersion method of the DatabaseMetaData interface.



STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
The Microsoft SQL Server 2000 Driver for JDBC recognizes a connection string property that is named SendStringParametersAsUnicode. The purpose of this property is to determine whether string parameters are sent to and from the SQL Server database in Unicode or in the default character encoding of the database. For the provided fix to work correctly, you also must set this connection string property to False (the default is True). For more information about the effects of setting this property, see the documentation for the driver. 

Steps to Reproduce the Problem
  Create the following stored procedure in your SQL Server database: CREATE proc largeCharProc @outData VARCHAR(8000) OUTPUT as --create a string with 8000 characters, all '*' Set @outData = REPLICATE('*', 8000)

--replace the 4000th character with M so you can easily spot it in the output Set @outData = STUFF(@outData, 4000, 1, 'M')

--replace the last character with X so you can easily spot it in the output Set @outData = STUFF(@outData, 8000, 1, 'X')   Compile, and then run the following Java code.

Note You must replace the server name, user id, and password in the connection string as appropriarte for your environment. import java.util.*; import java.sql.*; import java.io.*;

public class Class1 {   public static void main (String[] args) {       try {           Class.forName(&quot;com.microsoft.jdbc.sqlserver.SQLServerDriver&quot; ).newInstance; Connection connection1 = DriverManager.getConnection(&quot;jdbc:microsoft:sqlserver://myServer:1433;Databasename=myDatabase;SendStringParametersAsUnicode=False;&quot;,&quot;userid&quot;,&quot;password&quot;); CallableStatement pStmt = null; String strSQL = &quot;{call largeCharProc(?)}&quot;; pStmt = connection1.prepareCall(strSQL); pStmt.registerOutParameter(1, Types.VARCHAR); pStmt.execute; String outnesting = pStmt.getString(1); System.out.println(outnesting); System.out.println(String.valueOf(outnesting.length)); connection1.close; }       catch(Exception e)        { System.out.println(e.getMessage); }       try {           System.out.println(&quot;Press any key to quit....\n&quot;); System.in.read; }       catch (Exception ioe) {           System.out.println(&quot;caught&quot;); }   } }  Notice the output from the code. Before you apply the fix, you see that 4000 characters are returned. After you apply the fix, all 8000 characters are returned.

The version of the JDBC driver (2.2.0032) that fixes this problem also contains another fix. For additional information about another fix in the 2.2.032 version of the JDBC driver, click the following article number to view the article in the Microsoft Knowledge Base:

827221 FIX: Unsupported Data Conversion Error Occurs When You Error Occurs When You Use a Null Timestamp in JDBC

