亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

日夜難眠,看過jdbc之對面的女孩走過來的幫忙解答一點疑惑

日夜難眠,看過jdbc之對面的女孩走過來的幫忙解答一點疑惑

Bossen 2016-07-21 10:35:04
里面有個細節,困擾我12個小時了,我實在搞不明白,夜里做夢都在琢磨,希望有高人能指點,早釋疑惑。我在調用trans(from,to,amout)方法時,老師用的是Account from=null;Account to=null;from=accountDao.get(1);to=accountDao.get(2);這個里面的get()方法是怎么來的?直接設置from和to的Id為什么運行時報空指針錯誤?Account from=new Account();Account to=new Account();from.setId(1);to.setId(2);請求大師解答!跪求解答心中疑惑!
查看完整描述

4 回答

?
Bossen

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

//同時數據庫中的數據也同步更新了。

查看完整回答
1 反對 回復 2016-07-21
  • 慕粉3505864
    慕粉3505864
    不看看了想學著自己操作,但是發現老師這一集沒給完整代碼,請問你有沒有源碼分享一下?
?
Bossen

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呀,大家一起探討吧。

查看完整回答
反對 回復 2016-07-21
  • 4 回答
  • 1 關注
  • 1567 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號