1

CODE:

/*
 * 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!");
    }
  }

}

RUN:

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

QUESTION:Give me steps to solve it since database is connected already! What is the core of the problem? The problem is that if database postgresql connected then why not insert into database. The tables are also available and seen from netbeans. There needs to be a way to solve this run time exception issue when there is a query execution... So I needed step by step details to make it correct.

the pgAdmin shows the nodes_road_geoms column same as the netbeans shows up

  • not duplicate exactly because solution specifies maven project. I am not using a maven project so tell steps for a normal java project. Yes I think it has built issues of database versions but tell me the steps please – TALHA SHAHAB May 25 '18 at 05:58
  • How are you including the postgres driver dependencies? What other jars are included in your project? – Phil May 25 '18 at 06:00
  • The following jars are used in the project:rcaller 2.5, rcaller 3.0, github-java-sdk,org.json,postgris,postgresql 42.2.2,commons lang 2.6,common lang 3.1, common math 3.6.1, common lang 3.7, javax.servlet, javax.servlet 3.0, mysql connectior 8.0.1. – TALHA SHAHAB May 25 '18 at 06:19
  • please tell which jars to be used and which ones to remove and which new ones to be downloaded – TALHA SHAHAB May 25 '18 at 06:24
  • 1
    It sounds as if you have two PostgreSQL JDBC drivers on the class path, where a mix of classes of both drivers is used. Make sure that the postgis jar you use does not include an old PostgreSQL JDBC driver. This is BTW what the duplicate is trying to convey. – Mark Rotteveel May 25 '18 at 16:51
  • How do I check that, can u please tell me the steps please... – TALHA SHAHAB May 25 '18 at 17:45

2 Answers2

1

A SELECT statements has to be executed using executeQuery(). executeUpdate() is for DML statements like UPDATE, INSERT, or DELETE that don't normally return a ResultSet. Also, a DDL statement like CREATE TABLE can not be executed using executeQuery() you need execute() or executeUpdate() for that.

So your code should be:

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

And:

String query = "CREATE TABLE COMPANY(ID INT );";
s.execute(query);
connection.commit();
user207421
  • 305,947
  • 44
  • 307
  • 483
  • How would that cause a `NoSuchMethodError`? – Phil May 25 '18 at 06:01
  • @Phil Fixing this bug in the OP's code would cause that particular PostgresSQL code path not to be taken. Evidently it exposes a bug in the PostgresSQL JDBC driver. – user207421 May 25 '18 at 06:21
  • 1
    Didnot solve the problem brother. There is same error as mentioned above in the run section. Solve my problem of driver dependency please – TALHA SHAHAB May 25 '18 at 06:22
0

You have connection.setAutoCommit(false); and you didnt commit after performing update. You have to commit your transaction in order for changes to apply. You can also setAutoCommit(true);

Antoniossss
  • 31,590
  • 6
  • 57
  • 99