gpt4 book ai didi

java - 将 Microsoft SQL 与 Java 连接起来?

转载 作者:太空宇宙 更新时间:2023-11-04 13:38:36 27 4
gpt4 key购买 nike

我正在处理我的项目,我必须将 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();
}
}
}

如果有人知道我的代码有什么问题,请告诉我。

最佳答案

类路径中缺少 jar 文件。

将类路径设置为sqlserverjdbc.jar

其中包含com.microsoft.jdbc.sqlserver.sqlserverdriver类。

首先检查您的 jar 文件是否包含 com.microsoft.sqlserver.jdbc.SQLServerDrivercom.microsoft.jdbc.sqlserver.sqlserverdriver 类。

Check system requirements here

Both jdk 7 and jdk 8 compatible jar file

关于java - 将 Microsoft SQL 与 Java 连接起来?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31437958/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com