简体   繁体   English

SQL 服务器连接(JDBC)

[英]SQL server connection (JDBC)

i would like to connect the microsoft SQL server with java, using JDBC but i can't.我想使用 JDBC 将 microsoft SQL 服务器与 java 连接,但我不能。

I have done these steps:我已经完成了这些步骤:

  • download jdbc https://go.microsoft.com/fwlink/?linkid=2137600下载 jdbc https://go.microsoft.com/fwlink/?linkid=2137600
  • extraxt a zip file提取 zip 文件
  • add mssql-jdbc-8.4.1.jre14.jar to my eclipse IDE: --> propeties --> java build path --> classpath --> add external JAR --> selected mssql-jdbc-8.4.1.jre14.jar add mssql-jdbc-8.4.1.jre14.jar to my eclipse IDE: --> propeties --> java build path --> classpath --> add external JAR --> selected mssql-jdbc-8.4.1.jre14 .jar
  • in Microsoft SQL server configuration manager --> SQL server network configuration --> protocol for MCSQLSERVER --> tpc/ip --> enabled在 Microsoft SQL 服务器配置管理器中 --> SQL 服务器网络配置 --> MCSQLSERVER 协议 --> tpc/ip --> 启用
  • i created a SQL server authentication named theapplegeek我创建了一个名为 theapplegeek 的 SQL 服务器身份验证

java -version: java-版本:

openjdk version "15.0.1" 2020-10-20
OpenJDK Runtime Environment (build 15.0.1+9-18)
OpenJDK 64-Bit Server VM (build 15.0.1+9-18, mixed mode, sharing)

I wrote this code:我写了这段代码:

package sqlConn;

import java.sql.*;

public class connessione {
        public static void main(String[] args) {
            // Create a variable for the connection string.
            String connectionUrl = "jdbc:sqlserver://localhost:1344;databaseName=magazzino_automatica";
            String user = "theapplegeek";
            String password = "microsoftsqlservermerda";

            try (Connection con = DriverManager.getConnection(connectionUrl, user, password)) {
            } 
            // Handle any errors that may have occurred.
            catch (SQLException e) {
                e.printStackTrace();
            }
        }
}

error:

com.microsoft.sqlserver.jdbc.SQLServerException: La connessione TCP/IP all'host localhost, porta 1344 non è riuscita. Errore: "Connect timed out. Verificare le proprietà di connessione. Assicurarsi che un'istanza di SQL Server sia in esecuzione sull'host e accetti le connessioni TCP/IP alla porta. Verificare inoltre che le connessioni TCP alla porta non siano bloccate da un firewall.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:285)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2462)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:668)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:2695)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2362)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectInternal(SQLServerConnection.java:2213)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:1276)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:861)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at sql/sqlConn.connessione.main(connessione.java:12)

In your JDBC URL, change "1344" to "1433".在您的 JDBC URL 中,将“1344”更改为“1433”。 Also, make sure your firewall rules don't prevent the connection.此外,请确保您的防火墙规则不会阻止连接。 See https://docs.microsoft.com/en-us/sql/sql-server/install/configure-the-windows-firewall-to-allow-sql-server-access?view=sql-server-ver15请参阅https://docs.microsoft.com/en-us/sql/sql-server/install/configure-the-windows-firewall-to-allow-sql-server-access?view=sql-server-ver15

In SQL configuration manager -> SQL Server Network Configuration->Protocols for SQL Express-> Right click on TCP/IP you have to enable the TCP for that port. In SQL configuration manager -> SQL Server Network Configuration->Protocols for SQL Express-> Right click on TCP/IP you have to enable the TCP for that port. For some editions of MSSQL the TCP is disabled by default.对于某些版本的 MSSQL,默认情况下禁用 TCP。 Also make sure the TCP is enabled for your port Right click on TCP/Ip ->click properties->click IP addresses under IP all the value for TCP port should match to your port 1344. Also make sure the TCP is enabled for your port Right click on TCP/Ip ->click properties->click IP addresses under IP all the value for TCP port should match to your port 1344.

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

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