qqjudith 2015-04-23 01:27 采纳率: 62.5%
浏览 1711
已采纳

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

package com.huizhi.users.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.huizhi.database.DatabaseDAO;
import com.huizhi.users.action.Users;

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;
}
}

  • 写回答

5条回答 默认 最新

  • threenewbee 2015-04-23 01:44
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)