简体   繁体   English

sql server 2008不支持JDBC驱动程序类

[英]JDBC driver class not supported with sql server 2008

in my java code JDBC driver class is not supported with ms sql server 2008 while it works perfectly with ms sql server 2005 在我的Java代码中,ms sql server 2008不支持JDBC驱动程序类,尽管它与ms sql server 2005完美兼容

i have put jdbc driver class "com.microsoft.sqlserver.jdbc.SQLServerDriver" 我把jdbc驱动程序类“ com.microsoft.sqlserver.jdbc.SQLServerDriver”

MY CODE IS 我的代码是

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();
  }
 }
}    

while i am trying to connect it gives an error 当我尝试连接时出现错误

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)

plz help me to sort out this issue.. 请帮助我解决这个问题。

Please check your jdbc driver version. 请检查您的jdbc驱动程序版本。 "Microsoft JDBC Driver 4.0" is suitable for your ms sql server 2008. “ Microsoft JDBC驱动程序4.0”适合您的ms sql server 2008。

如果您使用的是SQL Server 2008,请将驱动程序类更改为-com.microsoft.jdbc.sqlserver.SQLServerDriver。连接应正常工作。

检查是否存在“ hj_cnb_ci”或DBName在您的数据库服务器上创建了它

Please check your database name. 请检查您的数据库名称。 There is no need to call newInstance() when you load the class; 加载类时无需调用newInstance() it will call its static block and call the registerDriver() method of the Driver manager class, and will pass its reference as a parameter. 它会调用其静态块并调用Driver Manager类的registerDriver()方法,并将其引用作为参数传递。

This method carries an object of the Driver class. 此方法带有Driver类的对象。 When you call the driver manager's getConnection() method, interally it will call drivers class method connection with the help of that object. 调用驱动程序管理器的getConnection()方法时,它将在该对象的帮助下调用驱动程序类方法连接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM