java express,用Java Express连接Java

I've designed a database management system using Java and MySQL. I used WAMP as the server. Everything was perfect with that..until now..

Now, I have to go for SQL Express instead of WAMP. I know nothing about SQL Express. I've installed it under defaults.

However, now I can connect to the database in Microsoft SQL Server Management Studio with following selections,

(LORDXAX-PC is my computer's name)

SRCDQ.png

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!

EDIT (2)

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;

}

This is the complete NetBeans output.

run:

START

java.sql.SQLException: Network error IOException: Connection refused: connect

at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(ConnectionJDBC2.java:417)

at net.sourceforge.jtds.jdbc.ConnectionJDBC3.(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.(SharedSocket.java:261)

at net.sourceforge.jtds.jdbc.ConnectionJDBC2.(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.

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=tru‌​e;domain=workgroup";

OR Another alternative is:

Download the [Microsoft JDBC driver][1]rather than JTDS.

Add sqljdbc4.jar to your classpath.

Update your connection string (JDBC URL) for you server

Use Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);

URL : jdbc:sqlserver://localhost;user=root;password=‌​123;"

If still problem, persist. Check with,

Enable TCP/IP network protocol, which is disable by default, and set the TCP/IP port to 1433 which is again default port no.

Open the SQL Server Configuration Manager

Start -> Microsoft SQL Server 2008 -> Configuration Tools -> SQL Server Configuration Manager

Then at the left hand tree. Select SQL Server 2005 Network Configuration-> Protocol for SQLEXPRESS-> TCP/IP.

Right click and enable it.

A window box appear on double click the TCP/IP. Click on the "IP Addresses" Tab

Set the TCP Port value to 1433 then click apply

Restart SQL Service.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值