加载了错误的驱动程序
我试图通过使用 jdbc 的 jtds1.2.jar 实现并在其上运行存储过程来连接到 sqlServer 2008。 我编写了一个测试应用程序,成功实现了这一点。
我在自己的环境(而不是应用程序服务器环境)中执行此操作时遇到问题。 我正在使用java 1.4。 我将类路径设置为首先查看 jtds。 当调用DriverManager.getConnection...方法时,源代码不是jtds源代码。 这是我的代码(与示例中的代码相同):
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url = "jdbc:jtds:sqlserver://" + serverIP + ":" + port + "/" + serviceName;
java.util.Enumeration myEnum = DriverManager.getDrivers();
Connection connection = DriverManager.getConnection(url,userName,password);
在调试中: myEnum 有 2 个驱动程序:
sun.jdbc.odbc.jdbcodbcdriver
jtds 驱动程序
恐怕 DriverManger 加载了第一个。
如何确保加载 jtds 驱动程序?
多谢
I am trying to connect to sqlServer 2008 by using jtds1.2.jar implementation of jdbc and running a stored procedure on it.
i wrote a test applications that did that successfully.
I have problem doing that in my own env(not application server enviorement).
Iam using java 1.4.
I set my classpath to see the jtds first.
when invoke the method DriverManager.getConnection... ,the source code is not jtds source code.
here is my code(it is the same code from the example):
Class.forName("net.sourceforge.jtds.jdbc.Driver");
String url = "jdbc:jtds:sqlserver://" + serverIP + ":" + port + "/" + serviceName;
java.util.Enumeration myEnum = DriverManager.getDrivers();
Connection connection = DriverManager.getConnection(url,userName,password);
in debuging: myEnum have 2 drivers:
sun.jdbc.odbc.jdbcodbcdriver
the jtds driver
i am afriad the the DriverManger loaded the first one.
How can i make sure to load the jtds driver?
Thanks alot
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
老实说,当您明确指定应该使用 jTDS 驱动程序时,我真的不明白为什么 DriverManager 会尝试使用 JDBC-ODBC 桥驱动程序。连接 URL 的第二部分是 jtds,它告诉 DriverManager 使用 jTDS 驱动程序。是什么让您如此确信 DriverManager 加载了 JDBC-ODBC 桥驱动程序?
在我的系统上,我还在枚举
DriverManager.getDrivers()
中看到了相同的两个条目。然而,仅仅因为 JDBC-ODBC 桥驱动程序在此列表中排在第一位,并且您可以通过它连接到 SQL Server,并不意味着 DriverManager 会选择它。您要求提供 jTDS 驱动程序,它会使用该驱动程序,或者根本不使用该驱动程序。如果您故意在
getConnection
调用中输入不正确的用户名和密码,会发生什么情况?当我这样做时,我得到了一个堆栈跟踪,清楚地表明它正在使用 jTDS:编辑:在您的评论中写道:
我不完全确定您的意思,但我猜测您正在使用调试器单步执行代码,并且当您尝试单步执行时使用
getConnection
方法时,调试器不会跳转到 jTDS 源代码。通常,调试器不会自动获取 JAR 的源代码 - 您必须将源代码“附加”到调试器。具体如何执行此操作取决于您使用的调试器。但是,我不确定您为什么要进入
getConnection
方法。我猜您在连接数据库时遇到了一些困难。您确定所有数据库连接详细信息都正确吗?您可以使用其他程序(例如 SQL Server Management Studio)连接到数据库吗?最后,(这是一个完整的猜测,但我以前也遇到过同样的问题),您是否正在尝试连接到 SQL Server 的 Express 版本?如果是这样,您需要在数据库上启用 TCP/IP 连接。 JDBC 使用 TCP/IP 连接来连接到 SQL Server,在 Express 版本上,这些连接默认处于禁用状态。请参阅http://softwaresalariman.blogspot.com/2007/ 04/jdbc-to-sql-server-express.html 了解如何执行此操作的说明。
I honestly cannot see why the DriverManager would attempt to use the JDBC-ODBC bridge driver when you have clearly specified that it should use the jTDS driver. The second part of the connection URL is
jtds
and it is this that tells the DriverManager to use the jTDS driver. What makes you so sure that the DriverManager loaded the JDBC-ODBC bridge driver?On my system, I also saw the same two entries in the enumeration
DriverManager.getDrivers()
. However, just because the JDBC-ODBC bridge driver comes first in this list, and because you could conceivably connect to SQL Server through it, doesn't mean that the DriverManager will choose it. You asked for the jTDS driver, and it'll use that or nothing at all.What happens if you deliberately put an incorrect username and password in the call to
getConnection
? When I did this, I got a stacktrace which clearly indicates it's using jTDS:EDIT: In your comment you wrote:
I'm not entirely sure what you mean here but I'm guessing that you're stepping through the code using a debugger and when you try to step into the
getConnection
method, the debugger doesn't jump into the jTDS source code. Normally, debuggers don't pick up source code for JARs automatically - you have to 'attach' the source code to the debugger. Precisely how you do this depends on what debugger you are using.However, I'm not sure why you want to step into the
getConnection
method. I'm guessing you're having some difficulty connecting to your database. Are you sure all the database connection details are correct? Can you connect to the database using another program (e.g. SQL Server Management Studio)?Finally, (this is a complete guess, but I've had the same issue before), are you attempting to connect to a Express edition of SQL Server? If so, you'll need to enable TCP/IP connections on the database. JDBC uses TCP/IP connections to connect to SQL Server, and on Express editions these connections are disabled by default. See http://softwaresalariman.blogspot.com/2007/04/jdbc-to-sql-server-express.html for instructions on how to do this.