java - TCP/IP(Java 应用程序到 SQL Server)

标签 java sql-server-2005 try-catch

我尝试知道是否可以使用这些代码将 Java 连接到 SQL Server:

package pkgtry;
import java.sql.*;
public class NewMain {

    public static void main(String[] args) {
    String  connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";


            Connection con = null;
            Statement stmt = null;
            ResultSet rs = null;

                try {

                    Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                        con = DriverManager.getConnection(connectionUrl);


                        String SQL = "";
                        stmt = con.createStatement();
                        rs = stmt.executeQuery(SQL);


                        while (rs.next()) {

                        }
                }


            catch (Exception e) {
                e.printStackTrace();
            }

            finally {
                if (rs != null) try { rs.close(); } catch(Exception e) {}
                    if (stmt != null) try { stmt.close(); } catch(Exception e) {}
                    if (con != null) try { con.close(); } catch(Exception e) {}
            }
    }
}

但在我这边它显示一个错误并显示:

com.microsoft.sqlserver.jdbc.SQLServerException: The TCP/IP connection to the host (local), port 1433 has failed. Error: "null. Verify the connection properties, check that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port, and that no firewall is blocking TCP connections to the port.".

这是什么意思?如何解决?

最佳答案

这意味着 JDBC 无法连接到数据库服务器。它找不到主机(local),您的意思是在这里放置localhost而不是(local)吗?

String  connectionUrl="jdbc:sqlserver://(local):1433;DatabaseName=OJT;user=sa;password=''";

关于java - TCP/IP(Java 应用程序到 SQL Server),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15756734/

相关文章:

java - return 语句后关闭结果集

java - 如何在后台使用 powershell 运行 java 类?

java - 更新 Lucene 索引中的文档时如何避免 OutOfMemoryErrors?

sql-server - SQL Server 用户与角色

sql-server - SQL Server 2005 - 在 XML 字段中搜索值

sql - 如何扫描两个查询之间的差异?

try 中的 pythonic 返回

c++ - 我可以制作一个#define 并一次性使用它吗?

java - 从 Java GUI 获取输入时出现问题

Python如何对非正数进行异常处理