当前位置: 首页 > 面试题库 >

从Java连接到SQL Server 2008

赵才俊
2023-03-14
问题内容

我正在尝试从Java连接到SQL Server 2008服务器

这是一个程序

import java.sql.*;

public class connectURL {

    public static void main(String[] args) {

        // Create a variable for the connection string.
        String connectionUrl = "jdbc:sqlserver://localhost/SQLEXPRESS/Databases/HelloWorld:1433;";// +
            //"databaseName=HelloWorld;integratedSecurity=true;";

        // Declare the JDBC objects.
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

            try {
                // Establish the connection.
                Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                    con = DriverManager.getConnection(connectionUrl);

                    // Create and execute an SQL statement that returns some data.
                    String SQL = "SELECT TOP 10 * FROM Person.Contact";
                    stmt = con.createStatement();
                    rs = stmt.executeQuery(SQL);

                    // Iterate through the data in the result set and display it.
                    while (rs.next()) {
                        System.out.println(rs.getString(4) + " " + rs.getString(6));
                    }
            }

        // Handle any errors that may have occurred.
        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 localhost/SQLEXPRESS/Databases/HelloWorld, 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.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:170)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1049)
    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(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at connectURL.main(connectURL.java:43)

我已经按照http://teamtutorials.com/database-tutorials/configuring-and-creating-a-
database-in-ms-sql-2008中
给出的所有说明进行了操作

可能是什么问题?


问题答案:

我认为连接网址可能有误。然后尝试以下连接,

 String Connectionurl="jdbc:sqlserver://localhost:1433;DatabaseName=YourDBName;user=UserName;Password=YourPassword"


 类似资料:
  • 我正在尝试使用同一版本的SolrJ从Java代码连接到运行5.3.1的Solr服务器。我在Eclipse中不断收到一条消息,HttpSolrServer已被弃用,但在任何地方都找不到它已被取代的内容。有人知道我如何使用SolrJ和当前的东西从Java连接到Solr吗?SolrJ文档似乎都表明HttpSolrServer是受支持的方式,但Eclipse对此并不满意。

  • 问题内容: 我正在尝试从Java连接到Hive服务器1。很久以前我在这个论坛上发现了一个问题,但这对我不起作用。我正在使用此代码: 这就是指南中显示的代码。我已经在.java的同一路径中复制了hive- metastore,service,jdbc,exec,core和更多.jar。当我编译它时,我得到以下消息: 有人知道这里发生了什么吗? 问题答案: 尝试 代替 希望您在代码中添加了语句

  • 我正在尝试将我的Java web项目连接到Firebase,这样我就可以将数据存储在FireStore中。我正在使用Java管理SDK,遵循官方指南:FireBaseAdminSDK。不幸的是,在初始化FirebaseAp时,我遇到了异常: 现在,我读到这里和那里,它的问题与guava项目,建议使用不同版本的guava库。我试过从18开始的所有版本。 我甚至下载了google firebase s

  • 我在将Netbeans中的Java与localhost中的mariadb连接时遇到问题。我已经看到一些类似的帖子,从Linux上NetBeans的Java应用程序连接到MariaDB(Mageia)但不起作用,这是非常相似的,所以我不知道为什么这个帖子不能帮助我。 我在/etc/my.cnf中选中了skip-networking指令,但它已经被注释了。

  • 问题内容: 码: CFG: 问题是我有一些密钥对,我用pkcs11-tool添加了它们。 softhsm的版本是1.2.1 为什么在密钥库中没有任何别名?如何解决这个问题? 问题答案: 我们将很快发布SoftHSM 1.3.0。它支持证书,并已通过Java测试。 SoftHSM.java: softhsm.cfg:

  • null 我确信我已经在127.0.0.1:27017启动了数据库服务,并且可以用shell和非异步方法连接。错误: PrimaryServerSelector没有从群集描述中选择服务器ClusterDescription{type=unknown,connectionmode=single,all=[ServerDescription{address=localhost:27017,type=u