开发者社区> 问答> 正文

SQLITE_ERROR SQL错误或缺少数据库(无此表:用户)

希望有人可以帮忙。我正在尝试从Eclipse中的动态Web项目访问数据库。由于某些原因,我继续收到以下错误:

[SQLITE_ERROR] SQL错误或缺少数据库(无此表:用户)

图书数据库中肯定有一个名为“用户”的表。我认为这可能与保存数据库的位置有关,因为我知道使用的代码可以正常工作,因为我在项目的另一部分使用了它。下面的屏幕快照显示了保存内容的位置。我的数据库在主目录和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<String,String> userPass = new HashMap<String,String>();

                // 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";
                }               
    }
}

展开
收起
几许相思几点泪 2019-12-10 19:33:51 1266 0
1 条回答
写回答
取消 提交回答
  • 检查下是否有其他程序占用数据库,如果已经有程序占用你再去打开就会抛出这种异常。

    2019-12-10 19:47:36
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载