try{
//taking input from user about how much balance
Scanner input = new Scanner(System.in);
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
String url = "jdbc:ucanaccess://c://Bibek//Atmcard.accdb";
System.out.print("nConnecting to database...");
con = DriverManager.getConnection(url);
st = con.createStatement();
System.out.println("n Enter balance you want to withdraw:n");
balance = Double.parseDouble(input.nextLine());
String sql = "select AccountBalance From Atm";
result = st.executeQuery(sql);
while(result.next()){
//assigning balanceFromDb to deduct and update in database
Double balanceFromDb = result.getDouble("AccountBalance");
balanceFromDb=balanceFromDb-balance;
result.updateDouble("AccountBalance", balanceFromDb);
result.updateRow();
}
}catch(Exception ex){
System.err.println(ex.toString());
}
输出:正在连接到数据库... 输入您要提取的余额:
20
net.ucanaccess.jdbc.UcanaccessSQLException:尝试分配给不可更新的列
检查访问数据库中的 Atm 对象,并确保它是一个表而不是查询。还要检查帐户余额的数据类型,并确保它是可编辑字段。如果它是自动递增或计算的,您将无法更新它。
编辑:看起来您必须将其声明为可更新的光标。这是来自 ucanacces 关于源代码锻造 http://ucanaccess.sourceforge.net/site.html 的示例 使用可更新的结果集
PreparedStatement ps = super.ucanaccess.prepareStatement( "SELECT * FROM T1", ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_UPDATABLE, ResultSet.CLOSE_CURSORS_AT_COMMIT);
rs = ps.executeQuery();
rs.next();
rs.updateString(2, "show must go off");
rs.updateRow();