java.sql.SQLException:用户“admin”登录失败

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27635877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 12:09:22  来源:igfitidea点击:

java.sql.SQLException: Login failed for user 'admin'

javasql-serverjtds

提问by EMM

Disclaimer: I have never used SQL server before.

免责声明:我以前从未使用过 SQL 服务器。

I am trying to connect to SQL server Express using java code.

我正在尝试使用 java 代码连接到 SQL server Express。

public class Test1 {

    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        Class.forName("net.sourceforge.jtds.jdbc.Driver");
        String url = "jdbc:jtds:sqlserver://localhost:1433/POC;instance=MOHITCH-LAPTOP/SQLEXPRESS";
        String user = "admin";
        String password = "admin";
        Connection conn = DriverManager.getConnection(url, user, password);
        Statement st = conn.createStatement ();
            ResultSet rs = st.executeQuery("select * from POC.dbo.poc_table");
            while (rs.next())
            {
                System.out.println(rs.getInt(1) + " " + rs.getString(2));
            }
        }
    }

And I am getting the exception:

我得到了例外:

Exception in thread "main" java.sql.SQLException: Login failed for user 'admin'.
    at net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
    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:352)
    at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
    at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at my.java.Test1.main(Test1.java:16)

I also tried logging in using MS SQL server Management Studio 2014. And I successfullydid it.

我还尝试使用 MS SQL Server Management Studio 2014 登录。我成功地做到了。

enter image description here

在此处输入图片说明

Here is my database structure:

enter image description here

这是我的数据库结构:

在此处输入图片说明

Any help is highly appreciated!!
Thanks

任何帮助都非常感谢!!
谢谢

回答by Ye Win

I think you need to modify some configuration in your server.

我认为您需要修改服务器中的一些配置。

Please follow the below step and hope this will help you.

请按照以下步骤操作,希望对您有所帮助。

1. Open your SQL Server Management Studio.

2. Database server right click and go to properties.

3. Choose Security option and check SQL Server and Windows authentication mode.

4. Enable TCP/IP connection in SQL Configuration Manager.

5. Restart your SQL server service.

回答by JJ Roman

First of all ensure that SQL Browser Service is running - in windows control panel services. If you can don't use JTDS driver there is Microsoft's official driver - according to different benchmarks it is slightly slower but it is the most comprehensive implementation - you will find a lot of problems with JTDS (something is not supported or simply not working and no one bothers to fix it, version 1.3 is not working in JDK6).

首先确保 SQL Browser 服务正在运行 - 在 windows 控制面板服务中。如果您不能使用 JTDS 驱动程序,则可以使用 Microsoft 的官方驱动程序——根据不同的基准测试,它会稍微慢一些,但它是最全面的实现——您会发现 JTDS 存在很多问题(某些东西不受支持或根本不起作用,没有人费心去修复它,1.3 版在 JDK6 中不起作用)。

Connection string, that is enough (instance not needed for express version):

连接字符串,这就足够了(快速版本不需要实例):

jdbc:jtds:sqlserver://localhost:1433/MyDatabase

If you used MS driver connection string would be:

如果您使用 MS 驱动程序连接字符串将是:

jdbc:sqlserver://localhost:1433;databaseName=MyDatabase