我試圖將數據插入到存儲在我的電腦, 對Access數據庫,但是當我運行此以下代碼的異常出現這個異常是什麼意思?? java.sql.SQLException:一般錯誤,在Java中用Access DB?
這是例外:
2011年12月14日7: 22:21 PM Ass3_lab.afterLogin $ 8的actionPerformed 重度:空 值java.sql.SQLException:常規錯誤 在sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6986) 在sun.jdbc.odbc.JdbcOdbc。 standardError(JdbcOdbc.java:7114) at sun.jdbc.odbc.JdbcOdbc.SQLExecDirect(JdbcOdbc.java: 3110) 在sun.jdbc.odbc.JdbcOdbcStatement.execute(JdbcOdbcStatement.java:338) 在sun.jdbc.odbc.JdbcOdbcStatement.executeUpdate(JdbcOdbcStatement.java:288)
我寫了這個方法,使插入:
public void saveDataInDB(int std_id,int course_id,String semester_no) throws SQLException {
Connection conn=null;
PreparedStatement pstmt=null;
String insertSQL="insert into Registered_course (std_id,course_id,semester_number) values(? , ?,?)";
try{
conn=con.getConnection();
pstmt=conn.prepareStatement(insertSQL);
pstmt.setInt(1,std_id);
pstmt.setInt(2,course_id);
pstmt.setString(3,semester_no);
pstmt.executeUpdate();
pstmt.close();
conn.close();
} // end of try
finally {
if(conn != null){
conn.close();
}
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception ignore) {
}
}
}
}
我打電話的動作監聽器內部這樣的方法:
save.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e){
try {
Login l = new Login();
for(int t=0;t<=numberOfRowsInTableOne;t++){
i.saveDataInDB(l.idFromDB,course_id[t],semester_no);
}
} catch (SQLException ex) {
Logger.getLogger(afterLogin.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
有人能幫助我解決這個問題嗎?
假設它的功課我相應地標記了它...... – mprabhat 2011-12-14 17:50:34
您正在關閉連接和語句兩次,請嘗試從「嘗試」中刪除並查看。也看到[這個問題](http://stackoverflow.com/q/4584951/922954) - 你可能也需要提交連接。 – aishwarya 2011-12-14 18:00:47