我设计了一个使用 Java 和 MySQL 的数据库管理系统。我使用WAMP作为服务器。一切都很完美……直到现在……
现在,我必须选择SQL Express而不是WAMP。我对 SQL Express 一无所知。我已经在默认情况下安装了它。
但是,现在我可以通过以下选择连接到Microsoft SQL Server Management StudioLORDXAX-PC
中的数据库,(是我的计算机名称)
但我需要将数据库与我的 Java 程序连接起来。(我之前已经成功使用过JDBC,与 WAMP 一起使用)
需要帮助才能成功??我试过搜索发现某些东西,但它给出了错误。有人可以为我提供干净的说明..
任何帮助表示赞赏。谢谢!
编辑 (2)
这是演示问题的完整代码..
package ExpressTest;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
public class MAIN {
public static void main(String[] args) {
new MAIN().start();
}
private void start() {
System.out.println("START");
String query = "SELECT * FROM logindata";
getData(query);
System.out.println("END");
}
public ResultSet getData(String query) {
Connection CON = null;
try {
Class.forName("net.sourceforge.jtds.jdbc.Driver");
CON = DriverManager.getConnection(URL, USER, PASS);
RESULT = CON.createStatement().executeQuery(query);
} catch (Exception ex) {
ex.printStackTrace();
}
return RESULT;
}
private String URL = "jdbc:jtds:sqlserver://localhost/brandix;instance=sqlexpress";
private String USER = "";
private String PASS = "";
private static ResultSet RESULT;
}
这是完整的NetBeans
输出。
run:
START
java.sql.SQLException: Network error IOException: Connection refused: connect
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:417)
at net.sourceforge.jtds.jdbc.ConnectionJDBC3.<init>(ConnectionJDBC3.java:50)
at net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:185)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at ExpressTest.MAIN.getData(MAIN.java:24)
at ExpressTest.MAIN.start(MAIN.java:16)
at ExpressTest.MAIN.main(MAIN.java:10)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
END
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:311)
at net.sourceforge.jtds.jdbc.SharedSocket.<init>(SharedSocket.java:261)
at net.sourceforge.jtds.jdbc.ConnectionJDBC2.<init>(ConnectionJDBC2.java:318)
... 7 more
BUILD SUCCESSFUL (total time: 7 seconds)