4 回答

TA貢獻13條經驗 獲得超4個贊
經過24小時的琢磨,終于弄懂了,我將我犯的錯誤放在這里,僅供大家參考,都是一些細節的錯誤,希望對大家有所幫助。
//“更新數據”方法
public void accountUpdate(Account a) throws Exception{
?Connection conn=DBUtil.getConnection();
?StringBuilder sb=new StringBuilder();
?sb.append("update account_info set account=?,amount=? where id=?");
?PreparedStatement ps=conn.prepareStatement(sb.toString());
?ps.setString(1,a.getAccount());//與上面sql語句對應放在第一個傳遞
?ps.setDouble(2, a.getAmount());//第二個傳遞
?ps.setInt(3, a.getId());//第三個傳遞
?ps.execute();
}
//獲取帶account、id、amount信息的Account對象
public Account get(Integer id) throws Exception{
?Connection conn=DBUtil.getConnection();
?StringBuilder sb=new StringBuilder();
?sb.append("select id,account,amount from account_info? where id= ?");
?PreparedStatement ps=conn.prepareStatement(sb.toString());
?ps.setInt(1, id);
?ResultSet rs=ps.executeQuery();
?Account a=null;
?while(rs.next()){
??a=new Account();
??a.setId(rs.getInt("id"));
??a.setAccount(rs.getString("account"));
??a.setAmount(rs.getDouble("amount"));
?}
?return a;
?
}
//調用方法
public class Test01 {
?public static void main ( String[] args ) throws Exception? {
??Service service=new Service();
??AccountAction a=new AccountAction();
??????? Account from=null;
??????? Account to=null;
??????? from=a.get(1);
??????? to=a.get(2);
??????? service.trans(from, to, 20d);
??????? System.out.println("賬戶Id:? "+from.getId()+"賬號:"+from.getAccount()+"????? 余額:"+from.getAmount());
??}
?}
//輸出結果
賬戶Id:? 1賬號:a????? 余額:210.0
//同時數據庫中的數據也同步更新了。

TA貢獻13條經驗 獲得超4個贊
定義get()方法,獲取from、to含有id,account、amount信息的對象
public Account get(Integer id) throws Exception{
?Connection conn=DBUtil.getConnection();
?StringBuilder sb=new StringBuilder();
?sb.append("select id,account,amount from account_info? where id= ?");
?PreparedStatement ps=conn.prepareStatement(sb.toString());
?ps.setInt(1, id);
?ResultSet rs=ps.executeQuery();
?Account a=null;
?while(rs.next()){
??a=new Account();
??a.setId(rs.getInt("id"));
??a.setAccount(rs.getString("account"));
??a.setAmount(rs.getDouble("amount"));
?}
?return a;
?}
//調用trans(from,to,amount)方法
public class Test01 {
?public static void main ( String[] args ) throws Exception? {
??Service service=new Service();
??AccountAction a=new AccountAction();
??????? Account from=null;
??????? Account to=null;
??????? from=a.get(1);
??????? to=a.get(2);
??????? service.trans(from, to, 20d);
??}
?}
運行的時候沒有問題,大神們過來看看,怎么破啊,雖然是個小問題,可是不解決,就不能真正掌握jdbc呀,大家一起探討吧。
添加回答
舉報