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;
}
}
catch (Exception ex) { }
這樣呢。
在你的方法上加上throws Exception