当前位置: 代码迷 >> J2SE >> 数据库 jdbc连接有关问题?
  详细解决方案

数据库 jdbc连接有关问题?

热度:528   发布时间:2016-04-24 02:15:01.0
数据库 jdbc连接问题?请指教。。。。。
package com.test.connectinDB;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class TestDB {
static Connection conn = null; // 声明Connection对象的实例
static PreparedStatement stmt = null; // 声明Statement对象的实例
static ResultSet rs = null; // 声明ResultSet对象的实例
static String dbClassName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";// 定义保存数据库驱动的变量
  static String dbUrl = "jdbc:microsoft:sqlserver://localhost:1433;databaseName=db_onLineMusic";
  static String dbUser = "sa";
  static String dbPwd = "";
public static void main(String args[])
{try{
Class.forName(dbClassName);
conn=DriverManager.getConnection(dbUrl, dbUser, dbPwd);
System.out.println("ok");
}catch(Exception e){
e.printStackTrace();
}
}

}

错误提示:java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC]Error establishing socket.
at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
at com.microsoft.jdbc.sqlserver.tds.TDSConnection.<init>(Unknown Source)
at com.microsoft.jdbc.sqlserver.SQLServerImplConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.getNewImplConnection(Unknown Source)
at com.microsoft.jdbc.base.BaseConnection.open(Unknown Source)
at com.microsoft.jdbc.base.BaseDriver.connect(Unknown Source)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:185)
at com.test.connectinDB.TestDB.main(TestDB.java:18)

那三个JAR包已经导入了,请大侠帮忙改下代码 或指出错误。。。。。

------解决方案--------------------
代码迷推荐解决方案:java环境变量设置,http://www.daimami.com/search?q=172921
------解决方案--------------------
我给你一个比较好的链接mysql的程序,你看下,用的不修改环境变量的方法,这个你是知道的。
Java code
package bao;import java.sql.Connection;import java.sql.DriverManager;import java.sql.SQLException;interface Lian {    public String DBDRIVER = "com.mysql.jdbc.Driver";    //public String DBDRIVER2 = "org.git.mm.mysql.Driver";//设置环境变量方法连接数据库    public String DBURl = "jdbc:mysql://localhost:3306/student";    public String DBUSER = "root";    public String DBPASSWD = "";//填写你的密码    public Connection getConn();};public class LianJdbc implements Lian {    private Connection conn = null;    public LianJdbc() {        try {            Class.forName(DBDRIVER);        } catch (Exception e1) {            e1.printStackTrace();        }        try {            conn = DriverManager.getConnection(DBURl, DBUSER, DBPASSWD);        } catch (SQLException e) {            e.printStackTrace();        }    }    public Connection getConn() {        return this.conn;    }};
  相关解决方案