为了账号安全,请及时绑定邮箱和手机立即绑定

为什么在 PostgreSQL 查询后没有执行部分 Java 代码?

为什么在 PostgreSQL 查询后没有执行部分 Java 代码?

子衿沉夜 2021-07-12 16:00:55
我正在做一个生成随机数据并用它填充 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 OneUser TwoUser Threeexample exampleexample exampleNo results were returned by the query.这是我注释掉该行时的输出: stmt2.executeQuery();User OneUser TwoUser Threeexample exampleexample exampleyespostgresBenerator
查看完整描述

1 回答

  • 1 回答
  • 0 关注
  • 195 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信