用Java Express连接Java [英] Connecting Java with SQL express
问题描述
我使用Java和MySQL设计了一个数据库管理系统。我使用 WAMP 作为服务器。一切都很完美..现在直到..
I've designed a database management system using Java and MySQL. I used WAMP as the server. Everything was perfect with that..until now..
现在,我必须选择 SQL Express 而不是 WAMP 。我对SQL Express一无所知。我在默认情况下安装了它。
Now, I have to go for SQL Express instead of WAMP. I know nothing about SQL Express. I've installed it under defaults.
但是,现在我可以使用以下选项连接到 Microsoft SQL Server Management Studio 中的数据库,
( LORDXAX-PC
是我的电脑名称)
However, now I can connect to the database in Microsoft SQL Server Management Studio with following selections,
(LORDXAX-PC
is my computer's name)
但是我需要将数据库与我的Java程序连接起来。 (之前我已成功使用 JDBC ,使用WAMP)
But I need to connect the database with my Java program. (I've used JDBC successfully before, with WAMP)
需要帮助才能成功吗?我试过找到某些东西,但它会给出错误。有人可以为我提供干净的说明..
Need help to make this a success?? I've tried searching found certain things, but it gives errors. Can someone provide me with clean instructions..
任何帮助表示赞赏。谢谢!
Any help is appreciated. Thanks!
这是一个完整的代码演示了这个问题..
This is a complete code demonstrating the issue..
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)
推荐答案
虽然是马有没有答案。我想给一个。
Though many answers are available. I would like to give one.
当您使用窗口身份验证时。
As you are using Window Authentication.
更改JTDS URL
Change JTDS URL from
private String URL = "jdbc:jtds:sqlserver://localhost/brandix;instance=sqlexpress";
TO
private String URL = "jdbc:jtds:sqlserver://localhost/brandix;instance=sqlexpress;useNTLMv2=true;domain=workgroup";
或另一种选择是:
下载[Microsoft JDBC驱动程序] [1]而不是JTDS。
将 sqljdbc4.jar
添加到类路径中。
Download the [Microsoft JDBC driver][1]rather than JTDS.
Add sqljdbc4.jar
to your classpath.
为您更新连接字符串(JDBC URL)服务器
Update your connection string (JDBC URL) for you server
使用 Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
URL: jdbc:sqlserver:// localhost; user = root; password = 123;
如果仍然有问题,请继续。检查,
If still problem, persist. Check with,
启用TCP / IP网络协议,默认情况下禁用,并设置TCP / IP端口为1433,这也是默认端口号。
Enable TCP/IP network protocol, which is disable by default, and set the TCP/IP port to 1433 which is again default port no.
-
打开SQL Server配置管理器
开始 - > Microsoft SQL Server 2008 - >配置工具 - > SQL Server配置管理器
Open the SQL Server Configuration Manager Start -> Microsoft SQL Server 2008 -> Configuration Tools -> SQL Server Configuration Manager
然后在左侧树中。为SQLEXPRESS选择SQL Server 2005网络配置 - >协议 - > TCP / IP。
Then at the left hand tree. Select SQL Server 2005 Network Configuration-> Protocol for SQLEXPRESS-> TCP/IP.
右键单击并启用它。
一个窗口双击TCP / IP时出现框。单击IP地址选项卡
A window box appear on double click the TCP/IP. Click on the "IP Addresses" Tab
设置TCP端口值为1433,然后单击应用
Set the TCP Port value to 1433 then click apply
重新启动SQL服务。
这篇关于用Java Express连接Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!