尝试使用 SQL Server Native Client 10.0 通过 ODBC 系统 DSN 从 Java 6 控制台应用程序连接到 Microsoft Windows Server 2008 R2 64 位系统上的 Microsoft SQL Server 2008 R2。以下源代码:
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String srcURL = "jdbc:odbc:FOO";
if (dbc == null)
{
dbc = DriverManager.getConnection(srcURL);
dbc.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
}
else
{
dbc.close();
dbc = DriverManager.getConnection(srcURL);
dbc.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
}
}
catch (ClassNotFoundException cx)
{
System.out.println("class not found");
}
catch (SQLException sx)
{
System.out.println("SQL Exception: " + sx);
log.info("SQL Exception: " + sx);
}
抛出错误
java.sql.exception [Microsoft] [ODBC Driver Manager] 字符串缓冲区长度无效
令人发指的是,相同的代码,以及以完全相同的方式配置的 ODBC 系统 DSN,适用于MS Server 2008 32 位(非 R2)和 MS SQL Server 2008 R2。两个系统之间的 Microsoft ODBC 驱动程序 dll 是不同的版本,6.0.xxxx 与 6.1.xxxx,我怀疑这是罪魁祸首。