简体   繁体   English

从Java连接到SQL服务器

[英]Connect to SQL server from Java

I'm trying to connect from SSMS(SQL Server Management Studio) to Eclipse(Java), but I keep getting this error message:我正在尝试从 SSMS(SQL Server Management Studio) 连接到 Eclipse(Java),但我不断收到此错误消息:

com.microsoft.sqlserver.jdbc.SQLServerException: The connection to the host localhost, named instance sqlexpress failed. Error: "java.net.SocketTimeoutException: Receive timed out". Verify the server and instance names and check that no firewall is blocking UDP traffic to port 1434.  For SQL Server 2005 or later, verify that the SQL Server Browser Service is running on the host.
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:234)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.getInstancePort(SQLServerConnection.java:6132)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.primaryPermissionCheck(SQLServerConnection.java:2609)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:2346)
    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 net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:16)

The code I do have is:我的代码是:


import java.sql.DriverManager;
import java.sql.SQLException;

//import com.sun.jdi.connect.spi.Connection;

public class JavaConnect2SQL {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String url = "jdbc:sqlserver://localhost\\SQLEXPRESS;databaseName=students"; // This is where I think the error is
        String user = "sa";
        String password = "123";
        try {
            java.sql.Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println("Succesfully connected to Microsoft SQL Server");
        }catch (SQLException e) {
            System.out.println("Oops! There was an error: ");
            e.printStackTrace();
        }
    }
        
}

I think the problem is in the connection URL, but I don't know the host name/instance name.我认为问题出在连接 URL 上,但我不知道主机名/实例名。 If you can tell me how to get my host name/instance name, that would be appreciated.如果您能告诉我如何获取我的主机名/实例名,将不胜感激。 But if the problem is something else, please tell me!但如果问题是别的,请告诉我!

Edit: I have changed the code, and I'm now getting a different.编辑:我已经更改了代码,现在有所不同。 Here is the new code:这是新代码:

package net.codejava.sql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JavaConnect2SQL {
    public static void main(String[] args) {

        // Create a variable for the connection string.
        String url = " jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students";
        String user= "sa";
        String password = "123";
        try {
            Connection connection = DriverManager.getConnection(url, user, password);
            System.out.println("Connection succesful!");
        }
        // Handle any errors that may have occurred.
        catch (SQLException e) {
            System.out.println("Here is your error message: ");
            e.printStackTrace();
        }
    }
}

The new error message is this:新的错误信息是这样的:

java.sql.SQLException: No suitable driver found for  jdbc:sqlserver://LAPTOP-5697KK36:1433;databaseName=students
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at net.codejava.sql.JavaConnect2SQL.main(JavaConnect2SQL.java:40)

I think the problem is that I installed the wrong version of JDBC Driver.我认为问题是我安装了错误版本的 JDBC 驱动程序。 My JRE version is 12.0.1.我的 JRE 版本是 12.0.1。 What is the correct JDBC Driver to install.什么是正确的 JDBC 要安装的驱动程序。

You can check in SQL server configuration manager and Microsoft SQL server management studio.可以登录SQL服务器配置管理器和微软SQL服务器管理工作室。 The state must be running in server configuration manager and you can try to connect your database in Microsoft SQL server management with your password. state 必须在服务器配置管理器中运行,您可以尝试使用您的密码连接 Microsoft SQL 服务器管理中的数据库。 if you connect successfully then there is another problem but it looks like the database is stopped.如果连接成功,则还有另一个问题,但看起来数据库已停止。

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

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