ms sql server 2008不支持JDBC驱动程序类,而它与ms sql server 2005完美配合
我已经把jdbc驱动程序类“com.microsoft.sqlserver.jdbc.SQLServerDriver”
我的代码是
import java.sql.*;
public class MysqlConnect{
public static void main(String[] args) {
System.out.println("MySQL Connect Example.");
Connection conn = null;
String url = "jdbc:sqlserver://SQLSERVERIP:1433;";
String dbName = "DatabaseName=DBNAME";
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String userName = "sa";
String password = "password";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+dbName,userName,password);
System.out.println("Connected to the database");
conn.close();
System.out.println("Disconnected from database");
} catch (Exception e) {
e.printStackTrace();
}
}
}
当我尝试连接时会出错
com.microsoft.sqlserver.jdbc.SQLServerException: Cannot open database "hj_cnb_ci
" requested by the login. The login failed.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError
(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.TDSTokenHandler.onEOF(tdsparser.java:246
)
at com.microsoft.sqlserver.jdbc.TDSParser.parse(tdsparser.java:83)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.sendLogon(SQLServerC
onnection.java:2529)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.logon(SQLServerConne
ction.java:1905)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.access$000(SQLServer
Connection.java:41)
at com.microsoft.sqlserver.jdbc.SQLServerConnection$LogonCommand.doExecu
te(SQLServerConnection.java:1893)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4615)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLSe
rverConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connectHelper(SQLSer
verConnection.java:1045)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.login(SQLServerConne
ction.java:817)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.connect(SQLServerCon
nection.java:700)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.
java:842)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at MysqlConnect.main(MysqlConnect.java:14)
请帮助我解决这个问题..
答案 0 :(得分:0)
请检查您的jdbc驱动程序版本。 “Microsoft JDBC Driver 4.0”适用于您的ms sql server 2008。
答案 1 :(得分:0)
如果您使用的是SQL Server 2008,请将您的驱动程序类更改为 - com.microsoft.jdbc.sqlserver.SQLServerDriver。连接应该可以正常工作。
答案 2 :(得分:0)
检查是否存在“hj_cnb_ci”或DBName在您的数据库服务器上创建它
答案 3 :(得分:0)
请检查您的数据库名称。加载课程时无需致电newInstance()
;它将调用其静态块并调用Driver manager类的registerDriver()
方法,并将其引用作为参数传递。
此方法包含Driver
类的对象。当您调用驱动程序管理器的getConnection()
方法时,它会在该对象的帮助下调用驱动程序类方法连接。