Connect to SQL Server 2012 using jTDs

☆樱花仙子☆ 提交于 2019-12-10 23:19:35

问题


Trying to connect to SQL Server 2012 (Express Edition) using JDBC.

Here is my code:

import java.sql.*;

public class MSSqlTestConnection {
    public static void main(String[] args) {
        try {
            Connection con = DriverManager.getConnection (
                "jdbc:jtds:sqlserver://localhost;instance=LOCALDB#B431115D;DatabaseName=foo","sa","my_password"
            );
            Statement stmt = con.createStatement();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

I have tried every possible combination available. Here is the full stack trace :

java.sql.SQLException: Login failed for user 'sa'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2988)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2421)
    at net.sourceforge.jtds.jdbc.TdsCore.login(TdsCore.java:632)
    at net.sourceforge.jtds.jdbc.JtdsConnection.<init>(JtdsConnection.java:371)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:184)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at MSSqlTestConnection.main(MSSqlTestConnection.java:10)

Pipe and TCP/IP is enabled

Mixed Authentication mode is enabled

Works fine with Management Studio when I login using "sa"

Ive pretty much tried everything. I cant seem to find a solution. I tried changing instance=SQLExpress. Removing instance property. Adding username and password property. Adding databasename property. Nothing works.

Any help would be appreciated.

SOLUTION :

I re-installed SQL Server 2012. Finally got to the working connection string. Turns out Microsoft SQL Server 2012 has changed the name of the default instance. It is now MSSQLSERVER instead of SQLExpress. You can choose your own instance i.e default or named while setting up SQL Server 2012.

Connection Connect = DriverManager.getConnection("jdbc:jtds:sqlserver://localhost:1433;instance=MSSQLSERVER;DatabaseName=Database_name",user,password);

Also don't forget to use mixed authentication mode while setting up SQL Server.


回答1:


I experienced the same symptoms after moving an application from MSSQL Server 2005 to MSSQL Server 2012.

My workaround was to change the connection string from

jdbc:jtds:sqlserver://a.example.com/xyz;user=bob;password=xxx

to

jdbc:jtds:sqlserver://a.example.com/xyz;instance=MSSQLSERVER;user=bob;password=xxx;TDS=7.0

For short, I added those two attributes at the end of the string:

;instance=MSSQLSERVER;TDS=7.0


来源:https://stackoverflow.com/questions/17190583/connect-to-sql-server-2012-using-jtds

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!