为什么在PostgreSQL查询后不执行Java代码的一部分?

时间:2018-08-15 12:29:44

标签: java postgresql execution

我正在做一个生成随机数据并用它填充PostgreSQL表的程序。您将在其中尝试执行三个简单SQL查询的代码中找到代码。前两个查询工作良好,但第三个查询则不行。实际上,第二次查询后我的部分代码似乎根本不会执行,因为控制台中未打印“是”。

我试图通过注释掉第二条查询执行行来编译代码,然后执行代码的结尾。有想法吗?

clickEvent

以下是输出:

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();

1 个答案:

答案 0 :(得分:1)

INSERT语句不能由executeQuery执行,而只能由executeUpdate执行。

还必须.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));
            }
        }
    }