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

时间:2014-12-24 11:04:52

标签: java sql-server jtds

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

我正在尝试使用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));
            }
        }
    }

我得到例外:

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)

我还尝试使用MS SQL服务器Management Studio 2014登录。我成功做了。

enter image description here

这是我的数据库结构:

enter image description here

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

2 个答案:

答案 0 :(得分:6)

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

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

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.

答案 1 :(得分:1)

首先确保SQL Browser Service正在运行 - 在Windows控制面板服务中。 如果你不能使用JTDS驱动程序,那就是微软的官方驱动程序 - 根据不同的基准测试它稍慢但是它是最全面的实现 - 你会发现JTDS存在很多问题(有些东西不支持或者根本不工作,没有人想要修复它,版本1.3在JDK6中不起作用。)

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

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

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

jdbc:sqlserver://localhost:1433;databaseName=MyDatabase
相关问题