[英]Problems while trying to connect Java with SQL Server Express
因此,我正在制作一個簡單的應用程序,其中我的代碼連接到 SQL Server Express 數據庫。 我都配置好了,JDBC,一個數據庫,一個登錄名和密碼。 但是當我嘗試運行代碼時,我不斷收到同樣的錯誤。 錯誤是:
java.sql.SQLException: No suitable driver found for jdbc:sqlserver://localhost;databaseName=SampleDatabase;user=testLogon;password=sample123
at java.sql.DriverManager.getConnection(DriverManager.java:689)
at java.sql.DriverManager.getConnection(DriverManager.java:270)
at App.main(App.java:10)
我目前正在使用 VSCode 進行開發,但我已經更改為 IntelliJ 和 Eclipse 並且我一直收到相同的錯誤。
我的代碼:
import java.sql.*;
public class App {
public static void main(String[] args) throws Exception {
String connectionUrl = "jdbc:sqlserver://localhost;databaseName=SampleDatabase;user=testLogon;password=sample123";
String insertString = "INSERT INTO Pessoa (id, nome, idade) VALUES (?, ?, ?)";
try (
Connection con = DriverManager.getConnection(connectionUrl);
PreparedStatement stmt = con.prepareStatement(insertString);
) {
Pessoa p1 = new Pessoa(1, "Maria", 50);
stmt.setInt(1, p1.getId());
stmt.setString(2, p1.getNome());
stmt.setInt(3, p1.getIdade());
stmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
JDBC jar 已導入
The problem is that you are using Java 8 (Java 8 update 241) to run your program, but are trying to use the version of the Microsoft SQL Server JDBC driver for Java 11 and higher (as indicated by the jre11
in the version).
Given the driver is compiled for Java 11, it cannot be loaded by Java 8. Download the Java 8 version of the driver (ending in jre8
), or upgrade to Java 11.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.