4 回答

TA貢獻(xiàn)13條經(jīng)驗(yàn) 獲得超4個(gè)贊
經(jīng)過(guò)24小時(shí)的琢磨,終于弄懂了,我將我犯的錯(cuò)誤放在這里,僅供大家參考,都是一些細(xì)節(jié)的錯(cuò)誤,希望對(duì)大家有所幫助。
//“更新數(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ǔ)句對(duì)應(yīng)放在第一個(gè)傳遞
?ps.setDouble(2, a.getAmount());//第二個(gè)傳遞
?ps.setInt(3, a.getId());//第三個(gè)傳遞
?ps.execute();
}
//獲取帶account、id、amount信息的Account對(duì)象
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()+"賬號(hào):"+from.getAccount()+"????? 余額:"+from.getAmount());
??}
?}
//輸出結(jié)果
賬戶Id:? 1賬號(hào):a????? 余額:210.0
//同時(shí)數(shù)據(jù)庫(kù)中的數(shù)據(jù)也同步更新了。

TA貢獻(xiàn)13條經(jīng)驗(yàn) 獲得超4個(gè)贊
定義get()方法,獲取from、to含有id,account、amount信息的對(duì)象
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);
??}
?}
運(yùn)行的時(shí)候沒(méi)有問(wèn)題,大神們過(guò)來(lái)看看,怎么破啊,雖然是個(gè)小問(wèn)題,可是不解決,就不能真正掌握jdbc呀,大家一起探討吧。
添加回答
舉報(bào)