我没有使用经验SQL Express
。所以我会尽量提供我认为重要的所有细节。
(这会使帖子有点长。但可能有一些不必要的细节,请浏览)
我最近安装了Microsoft SQL Server 2008
(开始菜单中有 2 项Microsoft SQL Server 2008
和Microsoft SQL Server 2008 R2
)
我需要与 SQL Express 数据库通信。实际上,我可以使用本地安装的数据库成功地做到这一点(我的这个线程,帮助了我)。但我需要在我们的办公室使用它。
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 Configuration Manager
。
该程序在这种情况下工作。所以看来我的程序MSSQLSERVER
(仅)使用了该服务。
但是在我的办公室电脑上,虽然SQL Express
版本是一样的(因为我用相同的设置安装在我的电脑上)我发现,这个MSSQLSERVER
服务在那里不可用。它仅显示上述部分服务。(SQL Server (SQLEXPRESS)
和SQL Server Browser
其他一些服务)
不幸的是,我无法更改我们办公室数据库的配置。所以我需要知道我是否可以更改我的程序以通过可用SQLSERVER
服务与数据库进行通信。
以下是一个简单(完整)的代码,以演示我如何尝试进行通信。(我正在使用Microsoft JDBC 驱动程序)
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;
}
}
我写这个是为了尝试更改连接字符串..
希望有办法解决这个问题。任何帮助都非常感谢..
谢谢!