JDBC cannot login to LocalDB instance, but in SSMS I can log in

时间秒杀一切 提交于 2019-12-01 22:55:27

问题


I am trying to connect to my database by JDBC on localhost. Connecting via windows authentication is no problem, but I want to connect via SQL authentication. Therefore, I created a login and a user corresponding to this login in my database. I can normally log in SSMS:

My connection string for JDBC: jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych;user=doszke;password=doszke123

Thrown exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'doszke'. ClientConnectionId:b7005fe3-904d-40c5-a89e-af0cb61250d6
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:254)
    at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:258)
    at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:104)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:4772)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:3581)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:81)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:3541)
    at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7240)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2869)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2395)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2042)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:1889)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1120)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:700)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:251)
    at main.Main.main(Main.java:38)

The username and password are the same, as those used for loging to SSMS.

Here my class code:

package main;

import java.sql.*;

public class Main {


    private static ResultSet selectStan(Connection connection) throws SQLException {
        String sql_stmt = "SELECT * FROM STAN;";
        Statement statement = connection.createStatement();
        ResultSet result = statement.executeQuery(sql_stmt);
        System.out.println("Select executed");
        return result;
    }


    public static void main(String[] args) {



        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }


        String userName = "doszke";
        String password = "doszke123";

        String url = "jdbc:sqlserver://localhost:1433;databaseName=TestBazyDanych;user=doszke;password=doszke123";


        try (Connection con = DriverManager.getConnection(url)) {

            if(con != null){
                System.out.println("connected");
            } else {
                System.out.println("unable to connect");
            }

        }
        catch (SQLException e) {
            e.printStackTrace();
        }

    }

}

回答1:


As Mark Rotteveel pointed out, I was trying to connect to a LocalDB instance with JDBC, which seemed undoable. (ref: here)

However, I installed jTDS and added to my classpath, changed my connection string to

jdbc:jtds:sqlserver://./TestBazyDanych;instance=LOCALDB#EB7165FD;namedPipe=true 

create a connection by the use of this connection string, username and password and it worked. The instance pipe number was taken from cmd line via

sqllocaldb i MSSQLLocalDB



回答2:


There are few things need to check:

  1. Did you create doszke user under the database and SSMS?
  2. Are you able to login with doszke/doszke123 credentials in SSMS?
  3. Please check 1433 port are open or not in your inbound and outbound firewall.
  4. Trying to telnet on localhost 1433. If it's getting failed change below setting: Go to Configuration tools -> SQL Server Configuration Manager Select SQL Server Network Configuration -> Select protocol in the right side window enable tcp/ip and restart the services in services.


来源:https://stackoverflow.com/questions/54266005/jdbc-cannot-login-to-localdb-instance-but-in-ssms-i-can-log-in

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