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 + ":" + por开发者_开发知识库t + "/" + 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
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:
java.sql.SQLException: Login failed for user 'x'. at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:368) at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2820) at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2258) at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:603) at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:345) at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50) at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184) at java.sql.DriverManager.getConnection(DriverManager.java:582) at java.sql.DriverManager.getConnection(DriverManager.java:185) ...
EDIT: In your comment you wrote:
when debuging the getconnection method the src code is not matched the jtds1.2 source code
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.
精彩评论