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

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

getConn下面劃了紅線,但是該怎么改呢?

getConn下面劃了紅線,但是該怎么改呢?

慕神8447489 2023-03-12 18:14:37
//在DBBean中有方法如下,數據庫連接public void getConnection(){String constr="jdbc:mysql://localhost/wodesql";String conuser="root";String conpass="";try{conn=DriverManager.getConnection(constr,conuser,conpass);}catch(SQLException se){se.printStackTrace();System.out.print("獲取數據庫連接失??!");}}  在servlet中,有//添加到購物車public boolean add(ArrayList<ItemsBean> cart){boolean mark=false;DBBean db=new DBBean();Connection conn=db.getConn();//這里,getConn下面劃了紅線,提示錯誤了,但是該怎么改呢?PreparedStatement pstmt=null;String sql="insert into s_ordertable(orderid,orderdate,username,realname,address,zipcode,telephone,paymoney,paymode,orderstate) values (?,?,?,?,?,?,?,?,?,?)";Iterator<ItemsBean> icart=cart.iterator();try{conn.setAutoCommit(false);pstmt=conn.prepareStatement(sql);
查看完整描述

2 回答

?
翻過高山走不出你

TA貢獻1875條經驗 獲得超3個贊

public void getConnection()這個方法你這么能設成void呢?必須返回一個connection啊,解決方法把void 改成Connection 然后在方法里面加上return conn
如下: 
public Connection getConnection(){
String constr="jdbc:mysql://localhost/wodesql";
String conuser="root";
String conpass="";
try{
conn=DriverManager.getConnection(constr,conuser,conpass);
}catch(SQLException se){
se.printStackTrace();
System.out.print("獲取數據庫連接失敗!");
}
return conn


另外db.getConn()這個是怎么回事?db里面有這個方法么?是db.getConnection()吧

查看完整回答
反對 回復 2023-03-15
?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

首先你dbBean的getConnection函數需要返回一個connection對象,然后才能用db.getConnection()得到conn啊

查看完整回答
反對 回復 2023-03-15
  • 2 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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