java - 为什么在 PostgreSQL 查询后没有执行部分 Java 代码?
问题描述
我正在做一个生成随机数据并用它填充 PostgreSQL 表的程序。您会发现我尝试执行三个简单 SQL 查询的代码。前两个查询运行良好,但第三个查询不行。实际上,在第二次查询之后,我的部分代码似乎根本没有执行,因为控制台中没有打印“是”。
我试图通过注释掉第二个查询执行行来编译我的代码,然后执行我的代码的结尾。任何想法?
import java.sql.*;
public class Main {
public static void main(String[] args) {
//connection to DB
Class.forName("org.postgresql.Driver");
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Benerator","postgres","newPassword");
//print the first two columns of table bank_card_people
PreparedStatement stmt = con.prepareStatement("select * from public.bank_card_people");
ResultSet res = stmt.executeQuery();
while(res.next()){
System.out.println(res.getString(1)+ " " + res.getString(2));}
//add a line to the same table
String SQL = "insert into public.bank_card_people (\"first-name\", \"last-name\", \"card-number\") VALUES ('example','example','example')";
PreparedStatement stmt2 = con.prepareStatement(SQL);
stmt2.executeQuery();
// is supposed to print all the databases
PreparedStatement stmt3 = con.prepareStatement("SELECT datname FROM pg_database WHERE datistemplate = false");
ResultSet res2 = stmt3.executeQuery();
System.out.println("yes");
while(res2.next()){
System.out.println(res2.getString(1));}
这是输出:
User One
User Two
User Three
example example
example example
No results were returned by the query.
这是我注释掉该行时的输出:stmt2.executeQuery();
User One
User Two
User Three
example example
example example
yes
postgres
Benerator
解决方案
INSERT 语句不能由executeQuery
but执行executeUpdate
。
Connection、Statement 和 ResultSet 也必须是.close()
d,最好使用 try-with-resources 来完成,即使出现异常或返回也能保证关闭。
它还有助于命名,因为它引入了新块。
//connection to DB
Class.forName("org.postgresql.Driver");
try (Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/Benerator","postgres","newPassword")) {
//print the first two columns of table bank_card_people
try (PreparedStatement stmt = con.prepareStatement("select * from public.bank_card_people");
ResultSet res = stmt.executeQuery()) {
while (res.next()) {
System.out.println(res.getString(1)+ " " + res.getString(2));
}
}
//add a line to the same table
String sql = "insert into public.bank_card_people (\"first-name\", \"last-name\", \"card-number\") VALUES ('example','example','example')";
try (PreparedStatement stmt2 = con.prepareStatement(sql)) {
int updateCount = stmt2.executeUpdate();
}
// is supposed to print all the databases
try (PreparedStatement stmt3 = con.prepareStatement("SELECT datname FROM pg_database WHERE datistemplate = false"));
ResultSet res2 = stmt3.executeQuery()) {
System.out.println("yes");
while(res2.next()){
System.out.println(res2.getString(1));
}
}
}
推荐阅读
- php - 如何在单击 save_progress 按钮(wordpress)时更新 acf 文件?
- electron - 在 MacOs Electron BrowserWindow.LoadURL 上不起作用
- repo - 如何理解 repo xml 中的“所有者”标签
- android - 未处理的异常:未处理的错误 Null 检查运算符用于空值,发生在 Flutter 中的“AuthBloc”实例中
- javascript - 使用 JavaScript 创建的 Svelte 组件不接收响应式更新
- reactjs - 盖茨比 mailchimp 的 cookie 有问题
- ffmpeg - ffmpeg api : av_interleaved_write_frame 在 linux 下返回错误损坏的管道
- django - URL 与 Django 中的 url 模式不匹配
- laravel - Chrome 使用了错误的本地 IP 地址
- r - 从热图中得出数值结论 - 空间数据