开发者社区> 问答> 正文

省略的catch方法应该怎么写?

public class UsersDAO {
private Connection con;
private PreparedStatement pt;
private ResultSet rs;
public boolean findUsers(String username){
boolean flag=false;
con=DatabaseDAO.getConnection();
try{
pt=con.prepareStatement("select*from users where username=?"); //获得PreparedStatement对象
pt.setString(1,username); //为参数赋值
rs=pt.executeQuery(); //执行查询语句,获取结果集
if (rs.next()){ //若果继续执行,表示用户名存在
flag=true;
}
}catch(SQLException e){ //捕获异常信息
e.printStackTrace();
}finally{
DatabaseDAO.closeRs(rs); //关闭相应资源
DatabaseDAO.closePt(pt); //关闭PreparedStatement
DatabaseDAO.closeCon(con); //关闭数据库连接
}
return flag;
}
public int save(Users users) {
// TODO Auto-generated method stub
int i=0;
con=DatabaseDAO.getConnection();
try{
pt=con.prepareStatement("inset into users(username,password,"+"name,nic,sex,age,email,phone,selfshow)values(?,?,?,?,?,?,?,?,?)");
pt.setString(1,users.getUsername());
pt.setString(2,users.getPassword());
pt.setString(3,users.getName());
pt.setString(4,users.getNic());
pt.setString(5,users.getSex());
pt.setInt(6,users.getAge());
pt.setString(7,users.getEmail());
pt.setString(8, users.getPhone());
pt.setString(9,users.getSelfshow());
i=pt.executeUpdate(); // 省略catch()和finally()方法
return i;
}
}

展开
收起
蛮大人123 2016-02-26 16:28:35 2159 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    catch (Exception ex) { }
    这样呢。在你的方法上加上throws Exception

    2019-07-17 18:48:23
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《0代码搭应用》 立即下载
不止代码 立即下载
低代码开发师(初级)实战教程 立即下载