java - 在哪里可以看到 HSQL 数据库和表

标签 java jdbc hsqldb

我已经下载了一个hsqldb.jar,我设置了project buildpath,然后接下来我写了程序

  Class.forName("org.hsqldb.jdbc.JDBCDriver");
Connection conn = DriverManager.getConnection(
                     "jdbc:hsqldb:mem:mydb", "SA", "");

String bookTableSQL = "create table MY_TABLE ("+
" TITLE varchar(256) not null primary key,"+
" AUTHOR varchar(256) not null"+
");";

Statement st = conn.createStatement();
st.execute(bookTableSQL);
System.out.println(st);
String sql = "INSERT INTO MY_TABLE " +
"VALUES ('Mahnaz', 'Fatma')";

st.executeUpdate(sql);

数据库和表创建成功。在下一步中,我插入了一个示例数据并获取了正在显示的数据

String sqlsel = "SELECT TITLE,AUTHOR FROM MY_TABLE";
 ResultSet rs = st.executeQuery(sqlsel);
 //STEP 5: Extract data from result set
 while(rs.next()){
    //Retrieve by column name
     String id  = rs.getString("TITLE");
     String age = rs.getString("AUTHOR");

    //Display values
    System.out.print("ID: " + id);
    System.out.print(", Age: " + age);

 }

我的问题是我没有创建“mydb”数据库。 另外在哪里可以看到创建的数据库和表?

最佳答案

您已经在内存中创建了数据库,因此没有包含您的表/数据的持久文件,然后您关闭了您的应用程序,所有数据都丢失了。

如果你想让它持久修复你的连接创建:将 mem 替换为 file。像这样:

Connection conn = DriverManager.getConnection("jdbc:hsqldb:file:mydb", "sa", "");

您可以阅读不同的 HSQLDB 模式 here .您还可以指定路径,您的数据库将在其中存储文件:

jdbc:hsqldb:file:/file/path/to/test"

第一次运行 HSQLDB 后会创建几个文件。您可以阅读的每个文件的用途 here .

test.properties Contains the entry 'modified'. If the entry 'modified' is set to 'yes' then the database is either running or was not closed correctly (because the close algorithm sets 'modified' to 'no' at the end).

test.script This file contains the SQL statements that makes up the database up to the last checkpoint - it is in synch with test.backup.

test.data This file contains the (binary) data records for CACHED tables only.

test.backup This is compressed file that contains the complete backup of the old test.data file at the time of last checkpoint.

test.log This file contains the extra SQL statements that have modified the database since the last checkpoint (something like the 'Redo-log' or 'Transaction-log', but just text).


顺便说一句,您可以使用此查询获取所有表:

SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_TYPE='TABLE'

您可以像普通查询一样执行此查询:

Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("SELECT * FROM INFORMATION_SCHEMA.SYSTEM_TABLES where TABLE_TYPE='TABLE'");

while(rs.next()) {
    ...
}

您还可以使用内置管理器查看您的数据库。您可以开始使用命令:

java -cp /path/to/hsqldb.jar org.hsqldb.util.DatabaseManager 

然后指定数据库路径:

jdbc:hsqldb:file:mydb

或者您可以使用流行的工具,如 SQuirreL .

关于java - 在哪里可以看到 HSQL 数据库和表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35240017/

相关文章:

java - 客户端关闭后保持套接字服务器打开

java - Libsvm java训练测试示例(也是实时的)

java - Hadoop不加载jdbc驱动

java - 事务看似回滚,但数据库中并没有回滚

回调中的 Java 数组数据损坏

java - 当您有名为 "Users"的表时出现 H2 JDBC 错误

java - 关于服务器中的 JDBC 连接

java - 无法启动 HSQLDB (java.net.BindException : Address already in use)

java - 预填充 HSQL 表

java - 为 gRPC ServerInterceptors 设置执行顺序