SHARE
TWEET

Untitled

a guest Sep 4th, 2018 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. JDBC   ScriptRunner : java.lang.NullPointerException
  2. import java.sql.*;
  3. import java.io.BufferedReader;
  4. import java.io.FileReader;
  5.  
  6. public class ConnectPostgreSQL {
  7.   public static void main(String[] argv) {
  8.   System.out.println("Checking if Driver is registered with DriverManager.");
  9.  
  10.   try {
  11.     Class.forName("org.postgresql.Driver");
  12.   } catch (ClassNotFoundException cnfe) {
  13.     System.out.println("Couldn't find the driver!");
  14.     System.out.println("Let's print a stack trace, and exit.");
  15.     cnfe.printStackTrace();
  16.     System.exit(1);
  17.   }
  18.  
  19.   System.out.println("Registered the driver ok, so let's make a connection.");
  20.  
  21.   Connection c = null;
  22.  
  23.   try {
  24.     c = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgres", "postgres", "passroot");
  25.   } catch (SQLException se) {
  26.     System.out.println("Couldn't connect: print out a stack trace and exit.");
  27.     se.printStackTrace();
  28.     System.exit(1);
  29.   }
  30.   System.out.println("Hooray! We connected to the PostgreSQL database!");
  31.  
  32.   long begin = System.currentTimeMillis();
  33.   System.out.println("==> Temps de départ : " + begin);
  34.  
  35.   ScriptRunner runner = new ScriptRunner(c, false, false);
  36.   try {
  37.     runner.runScript(new BufferedReader(new FileReader("C:/ScriptSQL/create.sql")));
  38.   } catch(Exception e) {
  39.     e.printStackTrace();
  40.   }
  41.  
  42.   long end = System.currentTimeMillis();
  43.   System.out.println("==> Temps d'arrivée : " + end);
  44.  
  45.   float time = ((float) (end-begin)) / 1000f;
  46.   System.out.println("Temps d'exécution : " + time + " sec");
  47.   }
  48. }
  49.    
  50. Checking if Driver is registered with DriverManager.
  51. Registered the driver ok, so let's make a connection.
  52. Hooray! We connected to the PostgreSQL database!
  53. ==> Temps de départ : 1300311132808
  54. java.lang.NullPointerException
  55.     at ScriptRunner.runScript(ScriptRunner.java:209)
  56.     at ScriptRunner.runScript(ScriptRunner.java:110)
  57.     at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
  58. java.lang.NullPointerException
  59.     at ScriptRunner.runScript(ScriptRunner.java:209)
  60.     at ScriptRunner.runScript(ScriptRunner.java:110)
  61.     at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
  62. java.lang.NullPointerException
  63.     at ScriptRunner.runScript(ScriptRunner.java:209)
  64.     at ScriptRunner.runScript(ScriptRunner.java:110)
  65.     at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
  66. java.lang.NullPointerException
  67.     at ScriptRunner.runScript(ScriptRunner.java:209)
  68.     at ScriptRunner.runScript(ScriptRunner.java:110)
  69.     at ConnectPostgreSQL.main(ConnectPostgreSQL.java:36)
  70. CREATE TABLE TEST123(a INTEGER, b VARCHAR(50), c VARCHAR(50))
  71. INSERT INTO TEST123 VALUES(1, 'Riri', 'Tic')
  72. INSERT INTO TEST123 VALUES(2, 'Fifi', 'Tac')
  73. INSERT INTO TEST123 VALUES(3, 'Loulou', 'Toc')
  74. ==> Temps d'arrivée : 1300311132938
  75. Temps d'exécution : 0.13 sec
  76.    
  77. try {
  78.    if (rs!=null)
  79.      rs.close();
  80. } catch (Exception e) {
  81.    e.printStackTrace();
  82. }
  83. try {
  84.    if (statement!=null)
  85.       statement.close();
  86. } catch (Exception e) {
  87.    e.printStackTrace();
  88. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top