我正在处理我必须将Microsoft SQL与Java连接的项目。我想从我的数据库中提取信息。我一直收到这个错误:
SEVERE: Java Runtime Environment (JRE) version 1.8 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
java.lang.UnsupportedOperationException: Java Runtime Environment (JRE) version 1.8 is not supported by this driver. Use the sqljdbc4.jar class library, which provides support for JDBC 4.0.
at com.microsoft.sqlserver.jdbc.SQLServerConnection.<init>(SQLServerConnection.java:304)
at com.microsoft.sqlserver.jdbc.SQLServerDriver.connect(SQLServerDriver.java:1011)
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at SendEmails.main(SendEmails.java:22)
我的司机出了点问题。我无法弄清楚是什么。也许我有错路或别的东西。这是我的代码:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class SendEmails {
public static void main(String[] args) {
Connection conn = null;
String dbName = "Student";
String serverip="109.45.100.100";
String serverport="1433";
String url = "jdbc:sqlserver://"+serverip+"\\SQLEXPRESS:"+serverport+";databaseName="+dbName+"";
Statement stmt = null;
ResultSet result = null;
String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String databaseUserName = "student";
String databasePassword = "admin";
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url, databaseUserName, databasePassword);
stmt = conn.createStatement();
result = null;
String pa,us;
result = stmt.executeQuery("Select Top 45 * From Students ");
while (result.next()) {
us=result.getString("uname");
pa = result.getString("pass");
System.out.println(us+" "+pa);
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果有人知道我的代码有什么问题,请告诉我。
答案 0 :(得分:0)
在类路径中缺少jar文件。
将classpath设置为sqlserverjdbc.jar
。
包含com.microsoft.jdbc.sqlserver.sqlserverdriver
类。
首先检查您的jar文件是否包含com.microsoft.sqlserver.jdbc.SQLServerDriver
或com.microsoft.jdbc.sqlserver.sqlserverdriver
类。
答案 1 :(得分:0)
例外情况&#34; java.lang.ClassNotFoundException: com.microsoft.jdbc.sqlserver.SQLServerDriver"
。
因此要么jar不存在,要么包含jar的路径有问题。
从https://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx下载jar并添加此jar作为依赖项。