作者:郑geraghty_926 | 来源:互联网 | 2023-06-02 15:01
希望有人可以提供帮助。我正在尝试从eclipse中的动态Web项目访问数据库。由于某些原因,我继续收到以下错误:
[SQLITE_ERROR] SQL错误或数据库丢失(无此表:用户)
books数据库中肯定有一个名为“ users”的表。我认为这可能与保存数据库的位置有关,因为我知道使用的代码可以正常工作,因为我在项目的另一部分使用了它。下面的屏幕快照显示了保存内容的位置。我的数据库在主目录和web inf目录中,因为我的主程序需要访问它以及服务器。
我的代码:
public class Authentication {
private static Connection getDBConnection() {
Connection cOnn= null;
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassnotFoundException e) {
System.out.println(e.getMessage());
}
try {
String url = "jdbc:sqlite:books.sqlite";
//Gets connection with the JDBC API
cOnn= DriverManager.getconnection(url);
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
public String authenticate(String username,String password) throws SQLException {
// TODO maybe removed this line below or change it.
Statement statement = null;
Connection cOnn= null;
ResultSet results = null;
HashMap userPass = new HashMap();
// Creates SQL query and adds it to String variable.
String query = "SELECT username,password FROM users";
// TODO Maybe remove the below line,not necessary?
// Book temp = null;
try {
cOnn= getDBConnection();
statement = conn.createStatement();
results = statement.executeQuery(query);
while (results.next()) {
String username = results.getString("username");
String Password = results.getString("password");
userPass.put(username,Password);
}
} finally {
if (results != null)
results.close();
if (statement != null)
statement.close();
if (conn != null)
conn.close();
}
//Returns to newly created booklist to where the method was called.
if (userPass.get(username)== password ) {
return "success";
}
else {
return "error";
}
}
}