开发者

JDBC + ScriptRunner : java.lang.NullPointerException

I code a class with a PostgreSQL JDBC driver and use the ScriptRunner class to use an external .sql script file. It works like expected but there is an annoying java.lang.NullPointerException in the output. It appears as often as the number lines in the SQL file.

import java.sql.*;
import java.io.BufferedReader;
import java.io.FileReader;

public class ConnectPostgre开发者_高级运维SQL {
  public static void main(String[] argv) {
  System.out.println("Checking if Driver is registered with DriverManager.");

  try {
    Class.forName("org.postgresql.Driver");
  } catch (ClassNotFoundException cnfe) {
    System.out.println("Couldn't find the driver!");
    System.out.println("Let's print a stack trace, and exit.");
    cnfe.printStackTrace();
    System.exit(1);
  }

  System.out.println("Registered the driver ok, so let's make a connection.");

  Connection c = null;

  try {
    c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "passroot");
  } catch (SQLException se) {
    System.out.println("Couldn't connect: print out a stack trace and exit.");
    se.printStackTrace();
    System.exit(1);
  }
  System.out.println("Hooray! We connected to the PostgreSQL database!");

  long begin = System.currentTimeMillis();
  System.out.println("==> Temps de départ : " + begin);

  ScriptRunner runner = new ScriptRunner(c, false, false);
  try {
    runner.runScript(new BufferedReader(new FileReader("C:/ScriptSQL/create.sql")));
  } catch(Exception e) {
    e.printStackTrace();
  }

  long end = System.currentTimeMillis();
  System.out.println("==> Temps d'arrivée : " + end);

  float time = ((float) (end-begin)) / 1000f;
  System.out.println("Temps d'exécution : " + time + " sec");
  }
}

Output :

Checking if Driver is registered with DriverManager.
Registered the driver ok, so let's make a connection.
Hooray! We connected to the PostgreSQL database!
==> Temps de départ : 1300311132808
java.lang.NullPointerException
    at ScriptRunner.runScript(ScriptRunner.java:209)
    at ScriptRunner.runScript(ScriptRunner.java:110)
    at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
java.lang.NullPointerException
    at ScriptRunner.runScript(ScriptRunner.java:209)
    at ScriptRunner.runScript(ScriptRunner.java:110)
    at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
java.lang.NullPointerException
    at ScriptRunner.runScript(ScriptRunner.java:209)
    at ScriptRunner.runScript(ScriptRunner.java:110)
    at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
java.lang.NullPointerException
    at ScriptRunner.runScript(ScriptRunner.java:209)
    at ScriptRunner.runScript(ScriptRunner.java:110)
    at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
CREATE TABLE TEST123(a INTEGER, b VARCHAR(50), c VARCHAR(50)) 
INSERT INTO TEST123 VALUES(1, 'Riri', 'Tic') 
INSERT INTO TEST123 VALUES(2, 'Fifi', 'Tac') 
INSERT INTO TEST123 VALUES(3, 'Loulou', 'Toc') 
==> Temps d'arrivée : 1300311132938
Temps d'exécution : 0.13 sec

I don't find how to get rid of these lines. :'(

Can somebody help me, please ?


I believe this might a bug in the ScriptRunner thing. In line 209 it assumes rs is not null (though previously checking if rs is actually null). My change would be:

try {
   if (rs!=null) 
     rs.close();
} catch (Exception e) {
   e.printStackTrace();
}
try {
   if (statement!=null) 
      statement.close();
} catch (Exception e) {
   e.printStackTrace();
}

My guess would be that the problem does appear for statements like CREATE TABLE or INSERT, while it doesn't for SELECTs. Am I right?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜