java - 连接 JAVA 与 SQLEXPRESS

标签 java sql jdbc

我没有使用 SQL Express 的经验。因此,我将尽力提供我认为重要的所有详细信息。
(这会使帖子有点长。但可能会有一些不必要的细节,请仔细阅读)

我最近安装了 Microsoft SQL Server 2008(开始菜单中有 2 个项目:Microsoft SQL Server 2008Microsoft SQL Server 2008 R2 )

我需要与 SQL Express 数据库通信。实际上,我可以使用本地安装的数据库成功完成此操作(我的 This thread 帮助了我)。但我需要在我们的办公室使用它。

这些计算机上安装了相同版本的 SQL Express。但我无法在那些机器上使用我的JAVA程序,它会抛出这种异常。

com.microsoft.sqlserver.jdbc.SQLServerException: 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.".
    at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
    at com.microsoft.sqlserver.jdbc.SQLServerException.ConvertConnectExceptionToSQLServerException(SQLServerException.java:241)
    at com.microsoft.sqlserver.jdbc.SocketFinder.findSocket(IOBuffer.java:2243)
    at com.microsoft.sqlserver.jdbc.TDSChannel.open(IOBuffer.java:491)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLServerConnection.java:1309)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConnection.java:991)
    at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerConnection.java:827)
    at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1012)
    at java.sql.DriverManager.getConnection(DriverManager.java:582)
    at java.sql.DriverManager.getConnection(DriverManager.java:207)
    at JDBC_TEST.TEST.getData(TEST.java:57)
    at JDBC_TEST.TEST$1.actionPerformed(TEST.java:44)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.java:6038)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
    at java.awt.Component.processEvent(Component.java:5803)
    at java.awt.Container.processEvent(Container.java:2058)
    at java.awt.Component.dispatchEventImpl(Component.java:4410)
    at java.awt.Container.dispatchEventImpl(Container.java:2116)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.java:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2429)
    at java.awt.Component.dispatchEvent(Component.java:4240)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)

尽管我尝试了一整天来解决这个问题,但还是找不到办法。但我发现了一些事情,认为它与此有关。

这是我的本地计算机 SQL Server 配置管理器 的屏幕截图。

enter image description here

程序在这种条件下工作。所以看来我的程序仅使用MSSQLSERVER服务。

但是在我的办公室计算机中,虽然 SQL Express 版本是相同的(因为,我使用相同的设置将其安装在我的计算机中),但我发现,这个 MSSQLSERVER那里不提供服务。它仅显示上述部分服务。 (SQL Server (SQLEXPRESS)SQL Server Browser 和其他一些服务)

不幸的是,我无法更改 Office 数据库的配置。因此我需要知道是否可以更改我的程序以通过可用的 SQLSERVER 服务与数据库进行通信。

以下是一个简单(完整)的代码,用于演示我如何尝试进行通信。 (我正在使用 Microsoft JDBC driver )

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

public class TEST extends JFrame {

    JTextField stringField;
    JButton tryButton;

    public static void main(String[] args) {
        new TEST().start();
    }

    private void start() {
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationByPlatform(true);
        getContentPane().setLayout(new GridLayout(2, 0));
        stringField = new JTextField("jdbc:sqlserver://localhost:1433;user=root;password=123;");
        tryButton = new JButton("TRY");
        getContentPane().add(stringField);
        getContentPane().add(tryButton);
        pack();
        tryButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    ResultSet rSet = getData(stringField.getText());
                    rSet.first();
                    JOptionPane.showMessageDialog(tryButton, rSet.getString("username"), "DONE !", JOptionPane.INFORMATION_MESSAGE);
                    rSet.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        setVisible(true);
    }

    public static ResultSet getData(String URL) throws ClassNotFoundException, SQLException {
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
        Connection CON = DriverManager.getConnection(URL);
        String query = "SELECT * FROM logindata;";
        Statement st = CON.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ResultSet RESULT = st.executeQuery(query);
        return RESULT;
    }
}

我写这篇文章是为了尝试更改连接字符串..

希望有办法解决这个问题。非常感谢任何帮助..
谢谢!

最佳答案

步骤

打开 Sql Server 配置管理器(开始 -> 程序 -> Microsoft SQL Server 2008 -> 配置工具)

展开 SQL Server 网络配置 -> [您的服务器实例]

双击 TCP/IP

根据协议(protocol):

确保“启用”为"is"

在 IP 地址下:

滚动到底部并在 IPAll 下设置 TCP 端口(默认为 1433)

找到您要连接的 IP 地址并将“启用”和“Activity ”设置为"is"

关于java - 连接 JAVA 与 SQLEXPRESS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12250771/

相关文章:

sql - Vertica UPDATE 语句中使用了哪种联接?

php - 在单个查询中查找 mysql 表中某个条目左侧和右侧的条目

java - 无法在 bluemix 上使用 java 注释初始化数据源

java - 将 mysql 连接到 Java netbeans 时出错

java - java中锁定文件

javascript - 了解 CryptoCompare 流数据

mysql - SQL查询问题-有六个表的困难查询

java - JPA - 如何在不构造父对象的情况下构造 ManyToOne 关系的子对象

java - JTextField 中的计算值错误

java - 如何使用 CachedRowSet 进行 SQL 查询?