美好的一天!我有以下代码连接到H2数据库并使用Java执行查询。但是,只有CALL CSVWRITE对我有用。
这是我的代码:
public static Result tagImport() {
String user = session("username");
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try {
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection(
"jdbc:h2:file:~/data/db", "sa", "");
statement = connection.createStatement();
resultSet = statement
.executeQuery("CALL CSVWRITE('textfiles/tags.csv', 'SELECT * FROM TAG');");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ok(dbSuccess.render(user));
}
public static Result tagExport() {
String user = session("username");
Connection connection = null;
ResultSet resultSet = null;
Statement statement = null;
try {
Class.forName("org.h2.Driver");
connection = DriverManager.getConnection(
"jdbc:h2:file:~/data/db", "sa", "");
statement = connection.createStatement();
resultSet = statement.executeQuery("MERGE INTO TAG (SELECT * FROM CSVREAD('textfiles/tags.csv'));");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return ok(dbSuccess.render(user));
}
如您所见,唯一的区别是要执行的查询。我对两者使用了相同的方法,但它似乎不适用于tagExport函数。我只想再次连接到数据库并将使用tagImport函数创建的csv文件插入表中。请帮助。谢谢!
答案 0 :(得分:1)
对于MERGE
声明,您需要使用Statement.executeUpdate()
代替executeQuery()