Java:缺少数据库错误 [英] Java: Missing Database error

查看:361
本文介绍了Java:缺少数据库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

  java.sql.SQLException:[SQLITE_ERROR] SQL错误或缺少数据库这样的表:公寓)

在现实中,表确实存在。以下是我的代码:

  try {
//使用当前类加载器加载sqlite-JDBC驱动程序
Class.forName(org.sqlite.JDBC);
Connection connection = null;
//创建数据库连接
connection = DriverManager.getConnection(jdbc:sqlite:/Path/To/apartments.db);
System.out.println(connection.getMetaData());
语句statement = connection.createStatement();
statement.setQueryTimeout(30);

ResultSet rs = statement.executeQuery(select * from apartments);
while(rs.next()){
//读取结果集
System.out.println(TEXT =+ rs.getString(display_text));
System.out.println(X1 =+ rs.getInt(x1));
}
} catch(Exception ex){
Logger.getLogger(MouseEventDemo.class.getName())。log(Level.SEVERE,null,ex);
}


解决方案

以获取正确的数据库路径



如何指定数据库文件是选择文件的示例 C:\work\mydatabase.db(在Windows中)

  Connection connection = DriverManager.getConnection(jdbc:sqlite:C:/work/mydatabase.db); 

UNIX(Linux,Mac OS X等)file / home / leo / work /mydatabase.db

 连接connection = DriverManager.getConnection(jdbc:sqlite:/ home / leo / work /mydatabase.db); 

如何使用内存数据库
SQLite支持内存数据库管理,它不创建任何数据库文件。要在Java代码中使用内存数据库,请按如下方式获取数据库连接:

 连接连接= DriverManager.getConnection(jdbc :sqlite :: memory:); 

这也可以帮助

  String path = this.getClass()。getResource(apartments.db)。getPath(); 
connection = DriverManager.getConnection(jdbc:sqlite:+ path);

假设apartments.db放在项目根目录中


I am getting the following error:

java.sql.SQLException: [SQLITE_ERROR] SQL error or missing database (no such table: apartments)

In reality, the table does exist. The following is my code:

try {
    // load the sqlite-JDBC driver using the current class loader
    Class.forName("org.sqlite.JDBC");
    Connection connection = null;
    // create a database connection
    connection = DriverManager.getConnection("jdbc:sqlite:/Path/To/apartments.db");
    System.out.println(connection.getMetaData());
    Statement statement = connection.createStatement();
    statement.setQueryTimeout(30);

    ResultSet rs = statement.executeQuery("select * from apartments");
    while(rs.next()) {
        // read the result set
        System.out.println("TEXT = " + rs.getString("display_text"));
        System.out.println("X1 = " + rs.getInt("x1"));
    }
} catch (Exception ex) {
    Logger.getLogger(MouseEventDemo.class.getName()).log(Level.SEVERE, null, ex);
}

解决方案

May be this can help you to get right path to you database

How to Specify Database Files

Here is an example to select a file C:\work\mydatabase.db (in Windows)

Connection connection = DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");

A UNIX (Linux, Mac OS X, etc) file /home/leo/work/mydatabase.db

Connection connection = DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");

How to Use Memory Databases SQLite supports on-memory database management, which does not create any database files. To use a memory database in your Java code, get the database connection as follows:

Connection connection = DriverManager.getConnection("jdbc:sqlite::memory:");

This also can help

String path=this.getClass().getResource("apartments.db").getPath();
            connection = DriverManager.getConnection("jdbc:sqlite:"+path);

assumes that apartments.db is put in project root directory

这篇关于Java:缺少数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆