读者朋友:
您好,用下面这段代码试一下:
private static String dbClassName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";// 定义保存数据库驱动的变量
private static String dbUrl = "jdbc:sqlserver://localhost:1433;DatabaseName=db_onLineMusic";
private static String dbUser = "sa";
private static String dbPwd = "";
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(dbClassName).newInstance();
conn = DriverManager.getConnection(dbUrl, dbUser, dbPwd);
} catch (Exception ee) {
ee.printStackTrace();
}
if (conn == null) {
System.err.println("警告: DBConnectionManager.getConnection() 获得数据库链接失败.\r\n\r\n链接类型:"
+ dbClassName
+ "\r\n链接位置:"
+ dbUrl
+ "\r\n用户/密码"
+ dbUser + "/" + dbPwd);
}
return conn;
}