我正在尝试从 Access DB(2010) 中的表中返回记录,但每次运行该语句时,我都会收到此错误:
"AWT-EventQueue-0" java.lang.NullPointerException
我初始化stmt的线程中的异常
我没有在这段代码中显示数据库的名称。这是我的代码:
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;
}
}