package it.com.db;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import sun.security.timestamp.TSRequest;
import it.com.com.Dbcon;
public class UserDao {
public List<Map> findAll(){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql ="SELECT * FROM users";
List<Map> list = new ArrayList<Map>();
try {
PreparedStatement pst=con.prepareStatement(sql);
ResultSet rs = pst.executeQuery();
while(rs.next()){
Map map = new HashMap();
map.put("userName", rs.getString("userName"));
map.put("userPwd", rs.getString("userPwd"));
map.put("Ename", rs.getString("Ename"));
map.put("Email", rs.getString("Email"));
map.put("Logo", rs.getString("Logo"));
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void resiger(String userName,String userPwd,String Ename,String Email,String Logo){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql="INSERT INTO users (userName,userPwd,Ename,Email,Logo) VALUES (?,?,?,?,?)";
try {
PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1,userName);
pst.setString(2,userPwd);
pst.setString(3,Ename);
pst.setString(4,Email);
pst.setString(5,Logo);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void update(String userName,String userPwd,String Ename,String Email,String Logo){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql="UPDATE users SET userPwd=?,Ename=?,Email=?,Logo=? WHERE userName=? ";
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, userPwd);
pst.setString(2, Ename);
pst.setString(3, Email);
pst.setString(4, Logo);
pst.setString(5, userName);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public int pagesize(){
List<Map> list = findAll();
int pagesize=list.size();
int n=0;
if(pagesize%5==0){
n=pagesize/5;
}else{
n=pagesize/5+1;
}
return n;
}
public int deleteByName(String userName){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql="DELETE FROM users WHERE userName=?";
int n=0;
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, userName);
pst.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
n=1;
}
return n;
}
public int login1(String username,String userpwd){
Dbcon db = new Dbcon();
Connection con=db.getcon();
String sql ="SELECT * FROM users WHERE userName=?";
int n=0;
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, username);
ResultSet rs = pst.executeQuery();
while(rs.next()){
n=1;//如果找到则执行
if(rs.getString("userPwd").equals(userpwd)){
n=2;//密码正确
}else{
n=3;//密码不正确
}
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return n;
}
public List<Map> select(String userName){
Dbcon db = new Dbcon();
Connection con=db.getcon();
List<Map> list =new ArrayList<Map>();
String sql="select * from users where userName=?";
try {
PreparedStatement pst=con.prepareStatement(sql);
pst.setString(1, userName);
ResultSet rs = pst.executeQuery();
if(rs.next()){
Map map = new HashMap();
map.put("userName", rs.getString("userName"));
map.put("userPwd", rs.getString("userPwd"));
map.put("Ename", rs.getString("Ename"));
map.put("Email", rs.getString("Email"));
map.put("Logo", rs.getString("Logo"));
list.add(map);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
UserDao us = new UserDao();
// List<Map> list=us.findAll();
// for(Map map:list){
// System.out.println(map);
// }
//us.update("x1", "xxx", "xxx", "xxx", "xxx");
//us.deleteByName("rt");
//us.resiger("asda", "sdad", "asda", "sdasd", "sdafdd", 221);
// List<Map>list1= us.select("rgt");
// for(Map map:list1){
// System.out.println(map);
// }
//
//us.resiger("x10", "xxx", "lala", "waa", "12");
}
}