package implement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import bean.UserInfo;
public class UserInfoImpl {
private Connection conn = null;
public UserInfoImpl(Connection conn){
this.conn = conn;
}
public int insert(UserInfo userinfo) {
int count = 0;
String sql = "insert into student(name,password,sex,age,addr,email)values(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userinfo.getName());
ps.setString(2, userinfo.getPassword());
ps.setString(3, userinfo.getSex());
ps.setInt(4, userinfo.getAge());
ps.setString(5, userinfo.getAddr());
ps.setString(6, userinfo.getEmail());
count = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return count;
}
public int delete(String idArr) {
String sql = "delete from student where id in (" + idArr + ")";
PreparedStatement ps = null;
ResultSet rs = null;
int rowNum = 0;
try {
ps = conn.prepareStatement(sql);
rowNum = ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return rowNum;
}
public boolean deleteRecord(int cid) {
String sql = "delete from student where id = ?";
PreparedStatement ps = null;
ResultSet rs = null;
int rowNum = 0;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, cid);
rowNum = ps.executeUpdate();
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
if(rowNum > 0){
return true;
}else{
return false;
}
}
public int update(UserInfo userinfo) {
int count = 0;
String sql = "update student set name = ?, password = ?, sex = ?, age = ?, addr = ?, email = ? where id = ?";
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, userinfo.getName());
ps.setString(2, userinfo.getPassword());
ps.setString(3, userinfo.getSex());
ps.setInt(4, userinfo.getAge());
ps.setString(5, userinfo.getAddr());
ps.setString(6, userinfo.getEmail());
ps.setInt(7, userinfo.getId());
count = ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return count;
}
public UserInfo query(String uname,String pwd) {
String sql = "select * from student where name=? and password=?";
UserInfo userinfo = new UserInfo();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
ps.setString(2, pwd);
rs = ps.executeQuery();
while(rs.next()){
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return userinfo;
}
public UserInfo queryById(int id) {
String sql = "select * from student where id = ?";
UserInfo userinfo = new UserInfo();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
while(rs.next()){
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return userinfo;
}
public boolean checkUser(String uname){
boolean flag = false;
String sql = "select * from student where name = ?";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
ps.setString(1, uname);
rs = ps.executeQuery();
while (rs.next()) {
flag = true;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return flag;
}
@SuppressWarnings({ "unchecked", "rawtypes" })
public List getSearch(String sql) {
List list = new ArrayList();
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
UserInfo userinfo = new UserInfo();
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
list.add(userinfo);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}
public ArrayList<UserInfo> queryPaging(int start,int size){
String sql = "select * from student limit ?,?";
PreparedStatement ps = null;
ResultSet rs = null;
ArrayList<UserInfo> al = new ArrayList<UserInfo>();
try {
ps = conn.prepareStatement(sql);
ps.setInt(1, start);
ps.setInt(2, size);
rs = ps.executeQuery();
while (rs.next()) {
UserInfo userinfo = new UserInfo();
userinfo.setId(rs.getInt("id"));
userinfo.setName(rs.getString("name"));
userinfo.setPassword(rs.getString("password"));
userinfo.setSex(rs.getString("sex"));
userinfo.setAge(rs.getInt("age"));
userinfo.setAddr(rs.getString("addr"));
userinfo.setEmail(rs.getString("email"));
al.add(userinfo);
}
}catch (Exception e) {
e.printStackTrace();
}finally{
try{
if(conn != null){
conn.close();
}
if(ps != null){
ps.close();
}
if(rs != null){
rs.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
return al;
}
public int queryRecord() {
String sql = "select count(*) from student";
PreparedStatement ps = null;
ResultSet rs = null;
int c = 0;
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
c = rs.getInt(1);
}
}catch (Exception
评论0
最新资源