【发布时间】:2021-04-01 14:22:36
【问题描述】:
我的主要课程:
public static void main(String[] args) {
Connection con = null;
Statement stmt = null;
int result = 0;
try {
Class.forName("org.hsqldb.jdbc.JDBCDriver");
con = DriverManager.getConnection("jdbc:hsqldb:file:data", "SA", "SA");
stmt = con.createStatement();
result = stmt.executeUpdate("CREATE TABLE surr (ID INTEGER IDENTITY NOT NULL, NAME VARCHAR(40) NOT NULL ) ");
con.commit();
} catch (Exception e) {
e.printStackTrace(System.out);
}
System.out.println(result + " rows effected");
System.out.println("Rows inserted successfully");
}
Pom xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>untitled</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>2.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
自动生成的data.script文件绝对拒绝建表。曾经可以创建一个表,甚至可以插入一行,但仅此而已。我发现如果你将行插入到一个奇迹般创建的表中,行被创建,它们可以在当前连接中获取,但是当 main 方法完成时,更改并没有保存在数据中。脚本。有什么办法呢?
【问题讨论】:
-
尝试在末尾添加一个 con.close() 以确保文件已写入。
-
看起来关闭是必要的,注意创建的表会显示 data.log,而不是 data.script