我正在学习有关将 SQLite 数据库连接到 Java 应用程序的教程。
当我运行该程序时,我在 NetBeans 控制台中收到以下错误:
run:
Error connecting to the databasejava.sql.SQLException: No suitable driver found for jdbc:C:\Users\lawman\Documents\Java Working Directory\LoginSql\src\project123.sqlite
BUILD SUCCESSFUL (total time: 0 seconds)
这是我的目录:
我在类 tobe Calledbymain
中有用于连接数据库的代码。
我在 mainclass
中有 main
,它创建了 tobe Calledbymain
的实例。
在我的库文件中,我导入了 sqlite-jdbcs.jar
。
这是tobe Calledinmain
的代码:
import java.sql.*;
public class tobecalledinmain {
public tobecalledinmain(){
Connection con = null;
Statement st=null;
ResultSet rs=null;
try
{
Class.forName("org.sqlite.JDBC");
con = DriverManager.getConnection("jdbc:C:\\Users\\lawman\\Documents\\"
+ "Java Working Directory\\LoginSql\\"
+ "src\\project123.sqlite");
st=con.createStatement();
//select all records from the table employee
//table has three firlds: employeeid,name and surname
rs=st.executeQuery("SELECT * FROM Employee;");
while (rs.next())
{
int id = rs.getInt("Employeeid");
String name = rs.getString("Name");
System.out.println("id = " + id);
System.out.println("name= " + name);
System.out.println();
}
rs.close();
st.close();
con.close();
}catch(Exception e)
{
System.out.println("Error connecting to the database" + e);
}
}
}
这是mainclass
代码:
public class mainClass {
public static void main(String[] args){
new tobecalledinmain();
}
}
;;
我不知道为什么我们需要分号!
无论如何,当教程结束时,他从控制台得到了结果。我收到上述错误消息。
错误消息中提到的驱动程序是什么以及如何获取它们?
最佳答案
您的 jdbc 连接字符串未指定 sqlite。试试这个,并使用正斜杠。
Connection con = DriverManager.getConnection("jdbc:sqlite:C:/PATH/TO/database.db");
关于java - java连接sqlite数据库的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33332639/