日期:2014-05-20  浏览次数:20783 次

数据库连接,困扰我三天了,高手救命啊!
SQL   server的sp3和sp4补丁都装了,驱动的三个jar包也拷到jre/lib/ext下了,环境变量也改了,可就是通不过,什么原因啊?

import   java.sql.*;
import   java.awt.*;


public   class   SQLConnClass  
{
public   static   void   main(String[]   args)  
{
try
{

System.out.println( "aa ");
Connection   Conn;
Class.forName( "net.sourceforge.jtds.jdbc.Driver ");
System.out.println( "bb ");
Conn   =   DriverManager.getConnection( "jdbc:jtds:sqlserver://localhost:1433/Test ",   "sa ",   "sa ");  
System.out.println( "数据库驱动程序注册成功 ");
}
catch(Exception   e)
{
System.out.println( "Error   Message: ");
e.printStackTrace();
System.out.println( "数据库连接失败 ");
}
}
}


aa
bb
Error   Message:java.sql.SQLException:   Network   error   IOException:   Connection   refused:   connect

at   net.sourceforge.jtds.jdbc.ConnectionJDBC2. <init> (ConnectionJDBC2.java:372)
at   net.sourceforge.jtds.jdbc.ConnectionJDBC3. <init> (ConnectionJDBC3.java:50)
at   net.sourceforge.jtds.jdbc.Driver.connect(Driver.java:178)
at   java.sql.DriverManager.getConnection(Unknown   Source)
at   java.sql.DriverManager.getConnection(Unknown   Source)
at   SQLConnClass.main(SQLConnClass.java:16)
Caused   by:   java.net.ConnectException:   Connection   refused:   connect
at   java.net.PlainSocketImpl.socketConnect(Native   Method)
at   java.net.PlainSocketImpl.doConnect(Unknown   Source)
at   java.net.PlainSocketImpl.connectToAddress(Unknown   Source)
at   java.net.PlainSocketImpl.connect(Unknown   Source)
at   java.net.SocksSocketImpl.connect(Unknown   Source)
at   java.net.Socket.connect(Unknown   Source)
at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native   Method)
at   sun.reflect.NativeMethodAccessorImpl.invoke(Unknown   Source)
at   sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown   Source)
at   java.lang.reflect.Method.invoke(Unknown   Source)
at   net.sourceforge.jtds.jdbc.SharedSocket.createSocketForJDBC3(SharedSocket.java:289)
at   net.sourceforge.jtds.jdbc.SharedSocket. <init> (SharedSocket.java:250)
at   net.sourceforge.jtds.jdbc.ConnectionJDBC2. <init> (ConnectionJDBC2.java:297)
...   5   more
数据库连接失败


------解决方案--------------------
Error Message:java.sql.SQLException: Network error IOException: Connection refused: connect
你先看看你1433端口好没的 netstat -na

------解决方案--------------------
net.sourceforge.jtds.jdbc.Driver 这个连接串没用过
一般都是用
public class conManager {
public conManager() {
}

public Connection GetCon() {
Connection con = null;
try {
Class.forName( "com.microsoft.jdbc.sqlserver.SQLServerDriver ");
String url =
"jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=news ";
con = DriverManager.getConnection(url, "sa ", " ");

} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return con;
}

public void CloseCon(Connection con1)