java - JDBC:简单的 MSSql 连接示例不起作用

标签 java jdbc

我正在学习 Java,只需要运行一些简单的东西来通过 JDBC 从 MSSQL 中检索一些数据。我书中的例子不起作用(但它已经有好几年了),下面这个来自 MS 的例子也不适合我:

http://msdn.microsoft.com/en-us/library/ms378956(v=sql.90).aspx

这是我的代码:

package javasql;
import java.sql.*;
import java.util.*;

public class Program {

    private static String url = "jdbc:sqlserver://localhost\\SQLExpress;database=Northwind;integratedSecurity=true;";
    //private static String userName = "sa";
    //private static String password = "myPassword";

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        RunDemo();
    }

    public static void RunDemo() {
        try {
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            Connection connection = DriverManager.getConnection(url);

            Statement statement = connection.createStatement();
            ResultSet results = statement.executeQuery("SELECT ProductName, Price FROM Products ORDER BY ProductName");

            while(results.next()) {
                System.out.println("Product Name: " + results.getNString("ProductName") + " Price: $" + results.getFloat("UnitPrice"));
            }

        } catch (ClassNotFoundException | SQLException ex) {
            System.out.println(ex.getMessage());
        }
    }
}

当我运行代码时,我没有抛出任何异常。我只是在输出窗口中得到这个:

run:
com.microsoft.sqlserver.jdbc.SQLServerDriver
BUILD SUCCESSFUL (total time: 0 seconds)

我使用的是 NetBeans 7.2。请有人给我一个工作示例。

编辑:

顺便说一句,对于您看到 \\SQLExpress 的连接字符串,我确实尝试将其删除并改用 instanceName=SQLExpress.. 但那没有'也没有任何作用。

编辑 2:

好的,我从 MS 下载了最新的 MSSQL JDBC 驱动程序并引用了其中的 2 个 JAR 文件。现在我得到了这个输出:

run:
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.
BUILD SUCCESSFUL (total time: 15 seconds)

Progress.. 至少我们现在可以看到它正在尝试连接,但是有人可以告诉我上述错误吗?

编辑 3:

修复了另外两个问题.. 一个是启用 SQL Server Browser,第二个是为 SQL Server 启用 TCP/IP。谢谢@Vikdor 现在我收到了这个错误:

run:
The TCP/IP connection to the host localhost, port 1433 has failed. Error: "Connection refused: connect. Verify the connection properties. Make sure that an instance of SQL Server is running on the host and accepting TCP/IP connections at the port. Make sure that TCP connections to the port are not blocked by a firewall.".
BUILD SUCCESSFUL (total time: 15 seconds)

我检查了 Windows 防火墙并添加了一个入站规则以允许该端口,但我仍然收到上述错误。有什么想法吗?

编辑 4:

尝试了此链接中的解决方案: http://www.coderanch.com/t/306316/JDBC/databases/SQLServerException-TCP-IP-connection-host

在 EDIT 3 中不再出现错误。现在出现另一个...

run:
Sep 21, 2012 11:33:16 AM com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
WARNING: Failed to load the sqljdbc_auth.dll cause : no sqljdbc_auth in java.library.path
This driver is not configured for integrated authentication. ClientConnectionId:577f359e-4774-45f3-96fb-588785911817
BUILD SUCCESSFUL (total time: 14 seconds)

现在对此感到非常厌倦.. 为什么是 Java,为什么?说真的……我很高兴我主要使用 .NET。好吧,当我找到解决方案时,我会将其发布在此处,以确保它可以帮助其他人,以免他们发疯,因为我即将...

编辑 5:

这有助于: java connecting to MicrosoftSQLServer 2005

我将目录路径放入我的 PATH 环境变量中。没用,所以我还把 sqljdbc_auth.dll 放到了我的 JDK 文件夹 C:\Program Files\Java\jdk1.7.0_04\bin 中。解决了。​​

最佳答案

好的,这就是解决我的问题的方法:

  1. 从这里下载最新的 MSSQL JDBC 驱动程序: http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx

  2. 在我的项目中引用了 2 个 JAR 文件: sqljdbc.jarsqljdbc4.jar(我还不确定是以上两个都需要还是只需要一个..)

  3. 确保 SQL Server Browser windows 服务正在运行

  4. 打开 SQL Server 配置管理器并转到 SQL Server 网络配置下的 Protocols for SQLEXPRESS。右键单击 TCP/IP 并选择属性。设置启用 = YES。

  5. 在那里,点击 IP 地址 标签并找到 IP All 部分。将 TCP 端口 设置为 1433。

  6. sqljdbc_auth.dll 添加到您的 PATH 环境变量。就我而言:D:\Java\sqljdbc_4.0\enu\auth\x64

  7. sqljdbc_auth.dll 复制到您的 JDK 目录。就我而言:C:\Program Files\Java\jdk1.7.0_04\bin

我希望这对某人有所帮助。

关于java - JDBC:简单的 MSSql 连接示例不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12523865/

相关文章:

java - 在 Lucene 中索引数据数组的更好方法是什么?

java - 在 Java 中高效格式化字符串数组

mysql - Jdbc 和 MySql 不想一起玩得很好

java - org.postgresql.util.PSQLException : FATAL: sorry, 客户端太多了

java - 数据库连接应该始终保持打开状态还是仅在需要时才打开?

java - Tapestry 5 - 将页面类名称与 URL 分离

java - 如何在 Eclipse 中将 Java 项目更改为 gwt Web 应用程序?

java - 我如何将 Spring Security 与我的 React-Spring Boot 应用程序一起使用

java - 如何为查询创建一个标志,以便如果添加记录,则显示 "successful",如果没有,则显示 "unsuccessful"

java - PreparedStatement IN 子句替代方案?