Komlan
Komlan

Reputation: 102

java select statement is not working

I'm trying to return records from a table in an Access DB(2010), but every time I run the statement I keep getting this error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException where I initialized stmt

I didnt display the name of the database in this code. Here is my code:

public class util {

static String path = "path to db";         
static String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ="+path;

  static Connection conn ; 
  static Statement s;

 public static boolean  ajouterFormule(String formule){
     boolean success = false;
      PreparedStatement stmt;
    try{
       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
       conn = DriverManager.getConnection(database, "", ""); 
       String ajout_sql = "INSERT INTO FORMULE(form_nom)"
             + "VALUES(?)";

       stmt= conn.prepareStatement(ajout_sql);
       stmt.setString(1, formule);
       int num_row = stmt.executeUpdate();
       if(num_row > 0)
           success =true;
    }
    catch(Exception e){
        System.out.println(e.getMessage());
             System.out.println("ERROR2");
    }
     return success;
 }


public static String[] getFormule()throws SQLException{
  String[] arr = new String[10];
   String getFormSql = "SELECT form_nom, form_statut "
            + "from formule";
    int i=0;
      System.out.println("NO");
    //Statement stmt = null;
    // ResultSet rs ;
    try{                   
        Statement stmt =  conn.createStatement();
      //  System.out.println(stmt);
        stmt.execute(getFormSql);
        ResultSet rs = s.getResultSet();
        while(rs.next()){
           String txt = rs.getString("form_nom");
    //       arr[i] = rs.getString("form_nom");
           System.out.println("Nothing..."+txt);

           i++;
        }
    }
    catch(SQLException e){
         System.out.println(e.getMessage());
          System.out.println("Error..");
    }
    System.out.print("Length "+ arr.length);

    return arr;
}

}

Upvotes: 0

Views: 1588

Answers (2)

vptheron
vptheron

Reputation: 7466

getFromSql is equal to "SELECT form_nom,form_statutfrom formule", you have to write :

String getFormSql = "SELECT form_nom,form_statut "
            + "from formule";

note the space ' ' after statut

Edit: Also when you do that

Statement stmt =  conn.createStatement();
      //  System.out.println(stmt);
        stmt.execute(getFormSql);
        ResultSet rs = s.getResultSet();

You read your ResultSet from a different statement than the one you just executed.

Edit bis: The way you initialize your connection is screwed up too. It is a static field, but you only initialize it if you call your insert method 'ajouterFormule' :

conn = DriverManager.getConnection(database, "", ""); 

you don't do it in your select method 'getFormule', so if you select without inserting first your connection will be null.

Remove your static fields (conn and s) and use local fields in each method for your connection and your statement (and also, you should close them once you're done inserting/selecting).

Upvotes: 2

BobTheBuilder
BobTheBuilder

Reputation: 19284

Looks like you haven't initialize conn anywhere.

You need to initialize it before accessing it.

Upvotes: 1

Related Questions