第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

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

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

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

4 回答

?
Bossen

TA貢獻13條經(jīng)驗 獲得超4個贊

經(jīng)過24小時的琢磨,終于弄懂了,我將我犯的錯誤放在這里,僅供大家參考,都是一些細(xì)節(jié)的錯誤,希望對大家有所幫助。

//“更新數(shù)據(jù)”方法
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語句對應(yīng)放在第一個傳遞
?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;
?
}

//調(diào)用方法

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());
??}
?}

//輸出結(jié)果

賬戶Id:? 1賬號:a????? 余額:210.0

//同時數(shù)據(jù)庫中的數(shù)據(jù)也同步更新了。

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

TA貢獻13條經(jīng)驗 獲得超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;
?}

//調(diào)用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呀,大家一起探討吧。

查看完整回答
反對 回復(fù) 2016-07-21
  • 4 回答
  • 1 關(guān)注
  • 1576 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號