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