为什么executeUpdate()函数不起作用?给出在正常项目而不是基于maven的项目中解决的步骤[重复]

问题描述 投票:1回答:2

码:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package aaa;
import static aaa.DB.geom;
import static aaa.DB.getConnection;
import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCExample {

  public static void main(String[] argv) throws SQLException {



    try {

      Class.forName("org.postgresql.Driver");
      // Class.forName("com.mysql.jdbc.Driver");

    } catch (ClassNotFoundException e) {

      System.out.println("Where is your PostgreSQL JDBC Driver? " +
        "Include in your library path!");
      e.printStackTrace();
      return;

    }

    System.out.println("PostgreSQL JDBC Driver Registered!");

    Connection connection = null;

    try {

      connection = DriverManager.getConnection(
        "jdbc:postgresql://localhost:5432/postgres", "postgres",
        "abc");

    } catch (SQLException e) {

      System.out.println("Connection Failed! Check output console");
      e.printStackTrace();
      return;

    }

    if (connection != null) {
      System.out.println("You made it, take control your database now!");

      //Connection conn = getConnection();


      connection.setAutoCommit(false);
      Statement s = null;
      try {
        s = connection.createStatement();
      } catch (Exception e) {
        System.out.println("statmnt connection not works");
      }
      PreparedStatement ss = connection.prepareStatement("SELECT * FROM nodes_road_geoms");
      try {
        ss.executeUpdate();
      } catch (Exception e) {
        System.out.println("statmnt excute update connection not works: ");
        e.printStackTrace();
      }
      String query = "CREATE TABLE COMPANY(ID INT );";

      ResultSet r = s.executeQuery(query);
      connection.commit();
    } else {
      System.out.println("Failed to make connection!");
    }
  }

}

跑:

-------- PostgreSQL JDBC Connection Testing ------------

PostgreSQL JDBC Driver Registered!

You made it, take control your database now!

Exception in thread "main" java.lang.NoSuchMethodError: 

org.postgresql.core.BaseConnection.getPreferQueryMode()Lorg/postgresql/jdbc/PreferQueryMode;
    at org.postgresql.jdbc.PgPreparedStatement.executeWithFlags(PgPreparedStatement.java:151)
    at org.postgresql.jdbc.PgPreparedStatement.executeUpdate(PgPreparedStatement.java:132)
    at aaa.JDBCExample.main(JDBCExample.java:69)
C:\Users\Dell\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

问题:由于数据库已连接,请给我解决它的步骤!这个问题的核心是什么?问题是,如果数据库postgresql连接,那么为什么不插入数据库。这些表格也可以从netbeans中看到。当查询执行时,需要有一种方法来解决此运行时异常问题...所以我需要一步一步的细节来使其正确。

java postgresql jdbc
2个回答
1
投票

必须使用SELECT执行executeQuery()语句。 executeUpdate()用于像UPDATEINSERTDELETE这样的DML语句,它们通常不返回ResultSet。此外,像CREATE TABLE这样的DDL语句不能使用executeQuery()执行,你需要execute()executeUpdate()

所以你的代码应该是:

PreparedStatement ss = connection.prepareStatement("SELECT * FROM nodes_road_geoms");
try {
   ResultSet rs = ss.executeQuery();
   while (rs.next() {
      // do something
   }
   rs.close();
} catch (Exception e) {
  System.out.println("statmnt excute update connection not works: ");
  e.printStackTrace();
}

和:

String query = "CREATE TABLE COMPANY(ID INT );";
s.execute(query);
connection.commit();

0
投票

你有qazxsw poi,并且在执行更新后你没有qazxsw poi。您必须提交交易才能应用更改。你也可以connection.setAutoCommit(false);;

© www.soinside.com 2019 - 2024. All rights reserved.