- 主题:java连接sqlite.jdbc运行错误问题
root@debian:~# more SQLiteExample.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLiteExample {
public static void main(String[] args) {
// SQLite connection string
String url = "jdbc:sqlite:sample.db";
// SQL statement for creating a new table
String createTableSQL = "CREATE TABLE IF NOT EXISTS users ("
+ "id integer PRIMARY KEY, "
+ "name text NOT NULL, "
+ "email text NOT NULL UNIQUE);";
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// Create a new table if it does not exist
stmt.execute(createTableSQL);
// Insert data into the users table
String insertSQL = "INSERT INTO users(name, email) VALUES ('Alice', 'alice@example.com')";
stmt.execute(insertSQL);
insertSQL = "INSERT INTO users(name, email) VALUES ('Bob', 'bob@example.com')";
stmt.execute(insertSQL);
// Query the users table
String selectSQL = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(selectSQL);
// Loop through the result set and print each row
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id"));
System.out.println("Name: " + rs.getString("name"));
System.out.println("Email: " + rs.getString("email"));
System.out.println("-----------------------");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
root@debian:~# javac SQLiteExample.java
root@debian:~# java SQLiteExample -cp /usr/share/java/sqlite-jdbc.jar
No suitable driver found for jdbc:sqlite:sample.db
root@debian:~# ls /usr/share/java/sqlite-jdbc.jar
/usr/share/java/sqlite-jdbc.jar
以上程序,哪里错了,为什么不能运行成功?sqlite,jdbc已经成功安装了的,就在/usr/share/java目录下。
--
FROM 183.161.82.*
报什么错?
【 在 zhangxp024 的大作中提到: 】
: root@debian:~# more SQLiteExample.java
: import java.sql.Connection;
: import java.sql.DriverManager;
: ...................
--
FROM 1.202.141.*
root@debian:~# java SQLiteExample -cp /usr/share/java/sqlite-jdbc.jar
No suitable driver found for jdbc:sqlite:sample.db
这行就是报错。
【 在 kawolu 的大作中提到: 】
: 报什么错?
:
--
FROM 183.161.94.*
能运行,没抛异常,说明代码没问题。
至于这个报错,显然是依赖的问题。
【 在 zhangxp024 的大作中提到: 】
: root@debian:~# java SQLiteExample -cp /usr/share/java/sqlite-jdbc.jar
: No suitable driver found for jdbc:sqlite:sample.db
: 这行就是报错。
: ...................
--
FROM 1.202.141.*
最开始加上这句代码: Class.forName("org.sqlite.JDBC");
--
FROM 103.131.153.*
try {
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:sqlite:"+filePath);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
--
FROM 210.75.217.*
AI也是这么说的,说 要加上如下代码。
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace();
// 如果抛出异常,说明驱动没有被正确引入或识别
}
修改之后的代码:
root@debian:~# more SQLiteExample.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SQLiteExample {
public static void main(String[] args) {
// SQLite connection string
String url = "jdbc:sqlite:sample.db";
// SQL statement for creating a new table
String createTableSQL = "CREATE TABLE IF NOT EXISTS users ("
+ "id integer PRIMARY KEY, "
+ "name text NOT NULL, "
+ "email text NOT NULL UNIQUE);";
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
e.printStackTrace();
// 如果抛出异常,说明驱动没有被正确引入或识别
}
try (Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement()) {
// Create a new table if it does not exist
stmt.execute(createTableSQL);
// Insert data into the users table
String insertSQL = "INSERT INTO users(name, email) VALUES ('Alice', 'alice@example.com')";
stmt.execute(insertSQL);
insertSQL = "INSERT INTO users(name, email) VALUES ('Bob', 'bob@example.com')";
stmt.execute(insertSQL);
// Query the users table
String selectSQL = "SELECT * FROM users";
ResultSet rs = stmt.executeQuery(selectSQL);
// Loop through the result set and print each row
while (rs.next()) {
System.out.println("ID: " + rs.getInt("id"));
System.out.println("Name: " + rs.getString("name"));
System.out.println("Email: " + rs.getString("email"));
System.out.println("-----------------------");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
}
}
运行:
root@debian:~# java SQLiteExample -cp /usr/share/java/sqlite-jdbc.jar
java.lang.ClassNotFoundException: org.sqlite.JDBC
at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:641)
at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:188)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:525)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:375)
at SQLiteExample.main(SQLiteExample.java:19)
No suitable driver found for jdbc:sqlite:sample.db
原因如上所说,应该是驱动没有被正确引入或识别。
我这是debian系统,通过如下方式安装sqlite-JDBC驱动:
root@debian:~# apt-cache search sqlite | grep jdbc
libxerial-sqlite-jdbc-java - Xerial's SQLite JDBC Driver
libxerial-sqlite-jdbc-jni - Xerial's SQLite JDBC Driver (jni)
root@debian:~# apt-get install libxerial-sqlite-jdbc-java
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
libxerial-sqlite-jdbc-java is already the newest version (3.40.1.0+dfsg-1+deb12u1).
0 upgraded, 0 newly installed, 0 to remove and 63 not upgraded.
root@debian:~# ls /usr/share/java/*sqlite* -l
lrwxrwxrwx 1 root root 22 Jun 13 2023 /usr/share/java/sqlite-jdbc.jar -> xerial-sqlite-jdbc.jar
lrwxrwxrwx 1 root root 22 Jun 13 2023 /usr/share/java/xerial-sqlite-jdbc-3.40.1.0.jar -> xerial-sqlite-jdbc.jar
-rw-r--r-- 1 root root 228931 Jun 13 2023 /usr/share/java/xerial-sqlite-jdbc.jar
【 在 bcllemon 的大作中提到: 】
: 最开始加上这句代码: Class.forName("org.sqlite.JDBC");
--
修改:zhangxp024 FROM 183.161.94.*
FROM 183.161.94.*
该怎么解决??
【 在 kawolu 的大作中提到: 】
: 能运行,没抛异常,说明代码没问题。
: 至于这个报错,显然是依赖的问题。
:
--
FROM 183.161.94.*
再问问AI
环境变量有问题?
【 在 zhangxp024 的大作中提到: 】
: 该怎么解决??
:
--
修改:ylh1969 FROM 221.218.60.*
FROM 221.218.60.*
AI挺聪明的,哈哈。原来是命令行写错了。
root@debian:~# java -cp /usr/share/java/sqlite-jdbc.jar:. SQLiteExample
ID: 1
Name: Alice
Email: alice@example.com
-----------------------
ID: 2
Name: Bob
Email: bob@example.com
-----------------------
【 在 ylh1969 的大作中提到: 】
: 再问问AI
: 环境变量有问题?
--
FROM 183.161.94.*