为什么这么多人都用Microsoft的驱动呢,不过Microsoft的驱动我也试过,也可以,不过我正常都用jtds.jar
这个文件一定要放在java_home\jre\lib\ext,也不知道什么原因,理论上放在WEB-INF/lib/下就可以了,classpath下都可以,给你一个代码,在你的机器上测试一下
import java.sql.*;
public class Dbtest {
Connection con;
Statement sta;
ResultSet rs;
String driver;
String url;
String user;
String pwd;
public Dbtest() {
driver = "net.sourceforge.jtds.jdbc.Driver";
url = "jdbc:jtds:sqlserver://127.0.0.1:1433/Northwind";
user = "sa";
pwd = "你的密码";
init();
}
public void init() {
try {
Class.forName(driver);
System.out.println("driver is ok");
con = DriverManager.getConnection(url, user, pwd);
System.out.println("conection is ok");
sta = con.createStatement();
rs = sta.executeQuery("select * from Categories");
while (rs.next())
System.out.println(rs.getString("CategoryName"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args [])
{
new Dbtest();
}
}