我在eclipse中连接sql数据库是写了如下代码:
private static String url="";
private static String user="";
private static String password="";
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test"; //test为数据库名
user = "test";//test为建的一个用户名
password = "";//没有设密码
} catch (InstantiationException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn = null;
try {
conn=DriverManager.getConnection(url,user,password);
JOptionPane.showMessageDialog(null,"连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
} 但是运行是出现下面的错误,请哪位高手尽快给我解决一下,非常感谢
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sdfi.edu.cn.tool.ConnectionBean.getConnection(ConnectionBean.java:34)
at sdfi.edu.cn.tool.ConnectionBean.main(ConnectionBean.java:43)
private static String url="";
private static String user="";
private static String password="";
static{
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test"; //test为数据库名
user = "test";//test为建的一个用户名
password = "";//没有设密码
} catch (InstantiationException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static Connection getConnection(){
Connection conn = null;
try {
conn=DriverManager.getConnection(url,user,password);
JOptionPane.showMessageDialog(null,"连接成功");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
} 但是运行是出现下面的错误,请哪位高手尽快给我解决一下,非常感谢
java.sql.SQLException: No suitable driver found for jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at sdfi.edu.cn.tool.ConnectionBean.getConnection(ConnectionBean.java:34)
at sdfi.edu.cn.tool.ConnectionBean.main(ConnectionBean.java:43)
驱动应该是com.microsoft.jdbc.sqlserver.SQLServerDriver
而不是:
com.microsoft.sqlserver.jdbc.SQLServerDriver
如果是2005的,就是url写错了,改成这样:jdbc:sqlserver://localhost:1433;DateBaseName=test;还有就是要确定 把驱程放到了构建路径中
url="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test"; //test为数据库名
换成:
url="jdbc:sqlserver://localhost:1433;DatabaseName=test"; //test为数据库名就可以通用!