E
E
Elzee2020-02-03 00:01:21
Java
Elzee, 2020-02-03 00:01:21

How to fix Can't use query methods that take a query string on a PreparedStatement error?

I want to add a new value to the table, but an error occurs, although if I go into the table itself, the values ​​are shown:
org.postgresql.util.PSQLException: Can't use query methods that take a query string on a PreparedStatement.
at org.postgresql.jdbc.PgPreparedStatement.executeQuery(PgPreparedStatement.java:95)
at M2.main(M2.java:22)
Database read error

What should I do in this case?

import java.sql.*;

public class M2 {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        String url = "jdbc:postgresql://localhost:5432/postgres";
        String userName = "postgres";
        String password = "12345";

        Class.forName("org.postgresql.Driver");

        try (Connection conn = DriverManager.getConnection(url, userName, password)) {
            PreparedStatement preparedStat = null;

            try {
                preparedStat = conn.prepareStatement("INSERT INTO books (name, price) VALUES (?, ?)");
                preparedStat.setString(1, "Shindler's List");
                preparedStat.setDouble(2, 32.5);
                preparedStat.execute();

                ResultSet rs = null;
                try {
                    rs = preparedStat.executeQuery("SELECT * FROM books");
                    while (rs.next()) {
                        int id = rs.getInt(1);
                        String name = rs.getString(2);
                        double price = rs.getDouble(3);
                        System.out.println("ID = " + id + "NAME = " + name + "PRICE = " + price);
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (rs != null) {
                        try {
                            rs.close();
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                    } else {
                        System.err.println("Ошибка чтения БД");
                    }
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                preparedStat.close();
            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Gornostaev, 2020-02-03
@yourdreambtw

The variable preparedStatstores the prepared request created by calling the method prepareStatement, it cannot be passed another request by calling executeQuery.

R
Roman, 2020-02-03
@Terran37

If the values ​​in the table are written, then the block for adding the record has worked, and the error is in the next block for displaying the results in a loop. Put the output in the system by its location of each value. So it will be clear to you on which line of the existing errors

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question