我想要的是从我保存在工作区文件夹中的数据库中获取数据,我还安装了最新的sqlite-jdbc jar ...
我在这里关注了http://www.tutorialspoint.com/sqlite/sqlite_java.htm 这是我的代码:
import java.sql.*;
import javax.swing.*;
public class sqliteConnection {
Connection conn = null;
public static Connection dbConnector(){
try{
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:TR.sqlite","","");
JOptionPane.showMessageDialog(null, "Connection Successful");
Statement stmt = null;
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM admin_tbl;");
while ( rs.next() ) {
int id = rs.getInt("admin_id");
String user = rs.getString("username");
String pass = rs.getString("password");
System.out.println( "ID = " + id );
System.out.println( "NAME = " + user );
System.out.println( "ADDRESS = " + pass );
System.out.println();
}
rs.close();
stmt.close();
return conn;
}catch(Exception e){
JOptionPane.showMessageDialog(null, e);
System.out.println(e);
return null;
}
}
}
首先我得到"连接成功"然后 我收到这个错误......
java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: admin_tbl)
答案 0 :(得分:0)
我认为您需要提供数据库文件的路径。目前,驱动程序管理器无法找到您所在的数据库。
试试这样:
String path=this.getClass().getResource("somedatabase.db").getPath();
conn = DriverManager.getConnection("jdbc:sqlite:"+path);
其中系统中数据库文件路径的路径。