package bistu.demo.dao.impl;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import bistu.demo.bean.Users;
import bistu.demo.dao.DaoUsers;
import bistu.demo.dbcon.ConnectionFactory;
public class ImpUsers implements DaoUsers {
private Connection conn=null;
private ResultSet rs=null;
public Users getUserByName(String name) {
String sql = "select userid,username,userpass,type,address,birthday,comment,degree,email,local,photo " +
"from users where username =?";
Users user = null;
PreparedStatement prst=null;
conn = ConnectionFactory.getConnection();
try {
prst=conn.prepareStatement(sql);
prst.setString(1, name);
rs=prst.executeQuery();
if(rs.next()){
user = new Users();
user.setAddress(rs.getString("address"));
user.setBirthDay(rs.getDate("birthDay"));
user.setComment(rs.getString("comment"));
user.setDegree(rs.getString("degree"));
user.setEmail(rs.getString("email"));
user.setLocal(rs.getString("local"));
user.setPhoto(rs.getString("photo"));
user.setUserId(rs.getInt("userid"));
user.setUserName(rs.getString("username"));
user.setUserPass(rs.getString("userpass"));
user.setType(rs.getString("type"));
} }catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
if(prst!=null) prst.close();
if(rs != null) rs.close();
if(conn != null) ConnectionFactory.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
@SuppressWarnings("unchecked")
public List<Users> getAllUsers() {
String sql = "select userid,username,userpass,type,address,birthday,comment,degree,email" +
",local,photo from users";
conn = ConnectionFactory.getConnection();
List <Users> users = new ArrayList();;
Users user=null;
try {
rs = conn.createStatement().executeQuery(sql);
while(rs.next()){
user=new Users();
user.setAddress(rs.getString("address"));
user.setBirthDay(rs.getDate("birthDay"));
user.setComment(rs.getString("comment"));
user.setDegree(rs.getString("degree"));
user.setEmail(rs.getString("email"));
user.setLocal(rs.getString("local"));
user.setPhoto(rs.getString("photo"));
user.setUserId(rs.getInt("userid"));
user.setUserName(rs.getString("username"));
user.setUserPass(rs.getString("userpass"));
user.setType(rs.getString("type"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally{
try {
if(rs != null) rs.close();
if(conn != null) ConnectionFactory.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return users;
}
public boolean deleteUserById(String userid) {
String sql="delete from users where userid= "+userid;
conn = ConnectionFactory.getConnection();
boolean result=false;
try {
result=conn.createStatement().execute(sql);
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
public Users getUserById(int id) {
String sql = "select userid,username,userpass,type,address,birthday,comment,degree,email,local,photo " +
"from users where userid =?";
Users user = null;
PreparedStatement prst=null;
conn = ConnectionFactory.getConnection();
try {
prst=conn.prepareStatement(sql);
prst.setInt(1, id);
rs=prst.executeQuery();
if(rs.next()){
user = new Users();
user.setAddress(rs.getString("address"));
user.setBirthDay(rs.getDate("birthDay"));
user.setComment(rs.getString("comment"));
user.setDegree(rs.getString("degree"));
user.setEmail(rs.getString("email"));
user.setLocal(rs.getString("local"));
user.setPhoto(rs.getString("photo"));
user.setUserId(rs.getInt("userid"));
user.setUserName(rs.getString("username"));
user.setUserPass(rs.getString("userpass"));
user.setType(rs.getString("type"));
} }catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
if(prst!=null) prst.close();
if(rs != null) rs.close();
if(conn != null) ConnectionFactory.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return user;
}
public boolean addUser(Users user) {
boolean result=true;
String sql = "insert into users (username,userpass,type,address,birthday,comment,degree,email,local,photo) values(?,?,?,?,?,?,?,?,?,?)";
PreparedStatement prst=null;
conn = ConnectionFactory.getConnection();
try {
prst=conn.prepareStatement(sql);
prst.setString(1, user.getUserName());
prst.setString(2, user.getUserPass());
prst.setInt(3, 1);
prst.setString(4,user.getAddress());
prst.setDate(5, user.getBirthDay());
prst.setString(6, user.getComment());
prst.setString(7, user.getDegree());
prst.setString(8, user.getEmail());
prst.setString(9, user.getLocal());
prst.setString(10,user.getPhoto());
result=prst.execute();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
if(prst!=null) prst.close();
if(rs != null) rs.close();
if(conn != null) ConnectionFactory.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
public boolean update(Users user) {
boolean result=true;
String sql = "update users set username=?,userpass=?,type=?,address=?,birthday=?,comment=?,degree=?,email=?,local=?,photo=? where userid=?";
PreparedStatement prst=null;
conn = ConnectionFactory.getConnection();
try {
prst=conn.prepareStatement(sql);
prst.setString(1, user.getUserName());
prst.setString(2, user.getUserPass());
prst.setInt(3, 1);
prst.setString(4,user.getAddress());
prst.setDate(5, user.getBirthDay());
prst.setString(6, user.getComment());
prst.setString(7, user.getDegree());
prst.setString(8, user.getEmail());
prst.setString(9, user.getLocal());
prst.setString(10,user.getPhoto());
prst.setInt(11,user.getUserId());
result=prst.execute();
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
try {
if(prst!=null) prst.close();
if(rs != null) rs.close();
if(conn != null) ConnectionFactory.close(conn);
} catch (SQLException e) {
e.printStackTrace();
}
}
return result;
}
}