postgresql:Can不使用在准备好的语句中获取查询字符串的查询方法

postgresql:Can't use query methods that take a query string on a prepared statement

我正在尝试创建一个插入到 postgresql 数据库中的 java 应用程序。 这是我的代码

import java .sql.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.util.*;

public class insert {
   public static void main(String args[])
 {

      try {
         Class.forName("org.postgresql.Driver");
         Connection c = DriverManager
            .getConnection("jdbc:postgresql://localhost:5432/testdb",
            "postgres", "saurabh");
         c.setAutoCommit(false);
         System.out.println("Opened database successfully");

         Scanner input = new Scanner(System.in);
         System.out.println("Enter the no.of records you want to enter");
         int n = input.nextInt();
         for(int i = 0; i<n ; i++)
{
        String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) VALUES ( ?, ?, ?, ? )";
         PreparedStatement pst = c.prepareStatement(sql);
         System.out.println("Enter id");
         int id= input.nextInt();
         pst.setInt(1, id);
         System.out.println("Enter name");
         String name = input.nextLine();
         pst.setString(2, name);
         System.out.println("Enter Address");
         String address = input.nextLine();
         pst.setString(3, address);
         System.out.println("Enter Salary");
         double salary = input.nextDouble();
         pst.setDouble(4, salary);
         pst.executeUpdate();
         pst.close();
}


         c.commit();
         c.close();
      } catch (Exception e) {
         System.err.println( e.getClass().getName()+": "+ e.getMessage() );
         System.exit(0);
      }
      System.out.println("Records created successfully");
   }
}

当我执行这个时,我得到一个错误
can't use query methods that take a query string on a prepared statement.

任何人都可以提出错误产生的原因吗?

你问的问题是你的查询有五列,你只提供了 4 个绑定值

(ID,NAME,AGE,ADDRESS,SALARY) VALUES ( ?, ?, ?, ? )

您跳过了下面的 age。此外,您需要使用 nextInt 留下的尾随换行符。而且,使用 PreparedStatement 的一个好处是可以进行批处理。我还会在 close 您的资源中添加一个 finally 块。像,

Connection conn = null;
PreparedStatement pst = null;
try {
    Class.forName("org.postgresql.Driver");
    conn = DriverManager.getConnection(
            "jdbc:postgresql://localhost:5432/testdb", "postgres",
            "saurabh");
    conn.setAutoCommit(false);
    System.out.println("Opened database successfully");

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the no.of records you want to enter");
    int n = input.nextInt();
    String sql = "INSERT INTO COMPANY (ID,NAME,AGE,ADDRESS,SALARY) "
            + "VALUES ( ?, ?, ?, ?, ? )";
    pst = conn.prepareStatement(sql);
    for (int i = 0; i < n; i++) {
        System.out.println("Enter id");
        int id = input.nextInt();
        pst.setInt(1, id);
        input.nextLine(); // <-- consume trailing newline.

        System.out.println("Enter name");
        String name = input.nextLine();
        pst.setString(2, name);
        System.out.println("Enter age");
        int age = input.nextInt();
        pst.setInt(3, age);
        input.nextLine(); // <-- consume trailing newline
        System.out.println("Enter Address");
        String address = input.nextLine();
        pst.setString(4, address);
        System.out.println("Enter Salary");
        double salary = input.nextDouble();
        pst.setDouble(5, salary);
        pst.addBatch();
    }
    pst.executeBatch();
    conn.commit();
} catch (Exception e) {
    System.err.println(e.getClass().getName() + ": " + e.getMessage());
    System.exit(0);
} finally {
    if (pst != null) {
        try {
            pst.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
System.out.println("Records created successfully");