简体   繁体   English

无法通过Java连接到SQL Server 2014

[英]Cannot connect to SQL Server 2014 via java

Question has been updated (The DriverManager is no longer loaded manually and instead the getConnection() method is used): 问题已更新(不再手动加载DriverManager,而是使用getConnection()方法):

package guii;

import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * This program demonstrates how to establish database connection to Microsoft
 * SQL Server.
 * @author www.codejava.net
 *
 */
public class JdbcSQLServerConnection {

public static void main(String[] args) {

    Connection conn = null;

    try {

        String dbURL = "jdbc:sqlserver://ASUS\\YES:1433";
        String user = "TestingUser";
        String pass = "12345";
        conn = DriverManager.getConnection(dbURL, user, pass);
        if (conn != null) {
            DatabaseMetaData dm = (DatabaseMetaData) conn.getMetaData();
            System.out.println("Driver name: " + dm.getDriverName());
            System.out.println("Driver version: " + dm.getDriverVersion());
            System.out.println("Product name: " + dm.getDatabaseProductName());
            System.out.println("Product version: " + dm.getDatabaseProductVersion());
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (conn != null && !conn.isClosed()) {
                conn.close();
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }
}
}

The problem is the resulting Exception of this code. 问题是此代码导致的异常。 I can't find out know the reason why that particular exception is thrown. 我找不到导致该特定异常的原因。

The username, password and servername were double checked and they are definitively correct. 用户名,密码和服务器名已被仔细检查,并且绝对正确。

Currently this exception is thrown: 当前抛出此异常:

com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user 'TestingUser'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:196)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerConnection.java:2532)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConnection.java:1929)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServerConnection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecute(SQLServerConnection.java:1917)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4026)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1416)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1061)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:833)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:716)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:841)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at guii.JdbcSQLServerConnection.main(JdbcSQLServerConnection.java:25)

Trouble Shooting Notes 故障排除注意事项

  1. Use integratedSecurity=true again 再次使用IntegratedSecurity = true

  2. Check if you have the sqljdbc_auch.dll in your system32 folder Check out this link Make sure you use the correct one (32 vs 64 bit) Make sure the sqljdbc_auch.dll is in the Windows system path 检查system32文件夹中是否有sqljdbc_auch.dll。检查此链接确保使用正确的链接 (32位和64位)确保sqljdbc_auch.dll在Windows系统路径中

  3. Check if the properties of your Server instance are set correctly: access rights, make sure it actually listens in the port 1433, ... check out this link and don't just look at the accepted answer but the other two as well. 检查您的Server实例的属性是否设置正确:访问权限,请确保它实际上在端口1433上进行侦听,...检查此链接 ,不仅查看已接受的答案,还要查看其他两个答案。

  4. make sure your firewall does not prevent any connections 确保您的防火墙没有阻止任何连接

  5. check out if your Java Version matches the jdbc Version you use sqljdbc4.2 needs Java 1.8, jdbc4.1 needs java 1.7. 检查您的Java版本是否与您使用的jdbc版本匹配sqljdbc4.2需要Java 1.8,jdbc4.1需要Java 1.7。 look here 看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM