eclipse添加了sqljdbc.jar,sqljdbc4.jar还是报com.microsoft.jdbc.sqlserver.SQLServerDriver,怎么解?
importjava.sql.*;publicclassDBConnectionManager{//SQLServerprivateStringdriverName="c...
import java.sql.*;
public class DBConnectionManager {
// SQLServer
private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";// 加载驱动程序
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";// 设置数据库连接串
private String user = "sa";// 数据库登录用户名
private String password = "0000";// 数据库登录密码
private static String message = "恭喜,数据库连接正常!";
public void setDriverName(String newDriverName) {
driverName = newDriverName;
}
public String getDriverName() {
return driverName;
}
public void setUrl(String newUrl) {
url = newUrl;
}
public String getUrl() {
return url;
}
public void setUser(String newUser) {
user = newUser;
}
public String getUser() {
return user;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getPassword() {
return password;
}
public Connection getConnection() {
try {
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
message = "数据库连接失败!";
return null;
}
}
public static void main(String[] args) {
try {
DBConnectionManager dcm = new DBConnectionManager();
Connection conn = dcm.getConnection();
System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
public class DBConnectionManager {
// SQLServer
private String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";// 加载驱动程序
private String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";// 设置数据库连接串
private String user = "sa";// 数据库登录用户名
private String password = "0000";// 数据库登录密码
private static String message = "恭喜,数据库连接正常!";
public void setDriverName(String newDriverName) {
driverName = newDriverName;
}
public String getDriverName() {
return driverName;
}
public void setUrl(String newUrl) {
url = newUrl;
}
public String getUrl() {
return url;
}
public void setUser(String newUser) {
user = newUser;
}
public String getUser() {
return user;
}
public void setPassword(String newPassword) {
password = newPassword;
}
public String getPassword() {
return password;
}
public Connection getConnection() {
try {
Class.forName(driverName);
return DriverManager.getConnection(url, user, password);
} catch (Exception e) {
e.printStackTrace();
message = "数据库连接失败!";
return null;
}
}
public static void main(String[] args) {
try {
DBConnectionManager dcm = new DBConnectionManager();
Connection conn = dcm.getConnection();
System.out.println(message);
} catch (Exception e) {
e.printStackTrace();
}
}
} 展开
1个回答
展开全部
private String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 加载驱动程序
你的写反了,仔细看
你的写反了,仔细看
追问
修改了还是报那错
追答
在SQL Server 2000 中加载驱动和URL路径的语句是
String driverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";
而SQL Server 2005 中加载驱动和url的语句则为
String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String dbURL = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=master";
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询