【发布时间】:2014-10-02 01:19:42
【问题描述】:
我正在尝试使用从摇摆 GUI 获得的信息更新一行。 这是我的例外:
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver]Error in row.BookFrame bUpdateActionPerformed
BookDB 类处理数据库交互和连接。
public class BookDB {
private final String data = "jdbc:odbc:Books";
private Connection con;
private Statement stmt;
ResultSet result;
BookDB() throws ClassNotFoundException, SQLException {
connect();
}
public static void main(String[] args) {
try {
BookDB b = new BookDB();
} catch (ClassNotFoundException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(BookDB.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void connect() throws ClassNotFoundException, SQLException {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
this.con = DriverManager.getConnection(data);
this.stmt = this.con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
this.result = this.stmt.executeQuery("SELECT Bookcode,Booktitle,Bookprice FROM Books");
} catch (ClassNotFoundException e) {
System.out.println("Failed to load jdbc/odbc drivers");
System.out.println("Class Erro: " + e);
} catch (SQLException e) {
System.out.println("Unable to connect");
System.out.println(("SQL ERROR: " + e));
}
}
public void updateRecord(Book book) throws SQLException {
result.updateString("BookCode", book.getCode());
result.updateString("BookTitle", book.getTitle());
result.updateDouble("BookPrice", book.getPrice());
result.updateRow();
}
}
BookFrame 处理事件处理和用户输入
public class BookFrame extends javax.swing.JFrame {
private BookDB bd;
public BookFrame() {
try {
initComponents();
this.bd = new BookDB();
bd.connect(); //connect to database
bd.result.next();
tCode.setText(this.bd.result.getString("bookcode") + ""); //sets textfields from data
tTitle.setText(this.bd.result.getString("booktitle") + "");
tPrice.setText(this.bd.result.getString("bookprice") + "");
} catch (ClassNotFoundException ex) {
Logger.getLogger(BookFrame.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(BookFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void bUpdateActionPerformed(java.awt.event.ActionEvent evt) {
try {
Book b = new Book(tCode.getText(), tTitle.getText(), Double.parseDouble( tPrice.getText())); //create book object of values to update with
bd.updateRecord(b);
} catch (SQLException ex) {
Logger.getLogger(BookFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
【问题讨论】:
-
Access 可能不支持可变结果集...如果支持,您需要将结果集的光标位置设置为无论如何要更新的行
-
@MadProgrammer 那么在这种情况下是否没有更新表的范围?
-
不是你的做法。即使 Access 确实支持可变的结果集,你这样做的方式也行不通。但是,您必须使用普通的、每天的普通更新语句进行手动更新......
-
(cc: @MadProgrammer ) 是的,Access ODBC 确实支持
CONCUR_UPDATABLE结果集。 -
@gord 因此,我使用“可能不是”的原因并指出即使它按照 OP 的方式进行操作也是错误的(尝试更新超出行集的 I 端!