时间原因,不能给你写一个,但可以给你个相似的,你可以看下
USER类:
public class User {
private int userid;
private String username;
private String password;
private String unum;
private String sex;
private String age;
private String tel;
private String purview;
private String address;
public int getUserid() {
return userid;
}
public void setUserid(int userid) {
this.userid = userid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUnum() {
return unum;
}
public void setUnum(String unum) {
this.unum = unum;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getTel() {
return tel;
}
public void setTel(String tel) {
this.tel = tel;
}
public String getPurview() {
return purview;
}
public void setPurview(String purview) {
this.purview = purview;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
userdao接口:
public int updateUserByUserID(User user);
public int addUser(User user);
public User queryUserById(int userid);
public int deleteUser(int id );
public List<User> queryAllUser(String username, int pageNumber, int pageSize);
public int queryAllUserCount(String username);
public int updatePassword(User user);实现类:public User loginUser(User user) {
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select * from shop_user where username=? and password=?";
User users=new User();
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
pstmt.setString(1,user.getUsername());
pstmt.setString(2, user.getPassword());
rs=pstmt.executeQuery();
if(rs.next()){
users.setUserid(rs.getInt("userid"));
users.setUsername(rs.getString("username"));
users.setPassword(rs.getString("password"));
users.setUnum(rs.getString("unum"));
users.setSex(rs.getString("sex"));
users.setAge(rs.getString("age"));
users.setTel(rs.getString("tel"));
users.setPurview(rs.getString("purview"));
users.setAddress(rs.getString("address"));
}else{
users = null;
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return users;
}
public int updateUserByUserID(User user) {
int rows=0;
PreparedStatement pstmt=null;
int i=0;
String sql="update shop_user set username=?,password=?,unum=?,sex=?,age=?,tel=?,purview=?,address=? where userid=?";
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
pstmt.setString(++i, user.getUsername());
pstmt.setString(++i, user.getPassword());
pstmt.setString(++i, user.getUnum());
pstmt.setString(++i, user.getSex());
pstmt.setString(++i, user.getAge());
pstmt.setString(++i, user.getTel());
pstmt.setString(++i, user.getPurview());
pstmt.setString(++i, user.getAddress());
pstmt.setInt(++i, user.getUserid());
rows=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rows;
}
public int addUser(User user) {
PreparedStatement pstmt=null;
String sql="insert into shop_user values(SEQ_SHOP_USER_USERID.Nextval,?,?,?,?,?,?,?,?)";
int row=0;
int i=0;
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
pstmt.setString(++i, user.getUsername());
pstmt.setString(++i, user.getPassword());
pstmt.setString(++i, user.getUnum());
pstmt.setString(++i, user.getSex());
pstmt.setString(++i, user.getAge());
pstmt.setString(++i, user.getTel());
pstmt.setString(++i, user.getPurview());
pstmt.setString(++i, user.getAddress());
row=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return row;
}
public User queryUserById(int userid) {
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select * from shop_user where userid=?";
User users=null;
int index=0;
users=new User();
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
pstmt.setInt(++index,userid);
rs=pstmt.executeQuery();
while(rs.next()){
users.setUserid(rs.getInt("userid"));
users.setUsername(rs.getString("username"));
users.setPassword(rs.getString("password"));
users.setUnum(rs.getString("unum"));
users.setSex(rs.getString("sex"));
users.setAge(rs.getString("age"));
users.setTel(rs.getString("tel"));
users.setPurview(rs.getString("purview"));
users.setAddress(rs.getString("address"));
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return users;
}
public int deleteUser(int id) {
int row=0;
PreparedStatement pstmt=null;
int i=0;
String sql="delete shop_user where userid=?";
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
pstmt.setInt(++i, id);
row=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return row;
}
public List<User> queryAllUser(String username, int pageNumber, int pageSize) {
List<User> list=new ArrayList<User>();
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select t.* from(select a.*,rownum r from(select * from shop_user order by userid desc ) a where 1=1";
StringBuffer sb=new StringBuffer();
sb.append(sql);
if(username!=null){
sb.append(" and username like '%");
sb.append(username);
sb.append("%')");
}else{
sb.append(")");
}
sb.append(" t where r>=");
sb.append(pageSize*(pageNumber-1)+1);
sb.append(" and r<=");
sb.append(pageSize*pageNumber);
sql=sb.toString();
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
rs=pstmt.executeQuery();
while(rs.next()){
User user=new User();
user.setUserid(rs.getInt("userid"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setUnum(rs.getString("unum"));
user.setSex(rs.getString("sex"));
user.setAge(rs.getString("age"));
user.setTel(rs.getString("tel"));
user.setPurview(rs.getString("purview"));
user.setAddress(rs.getString("address"));
list.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
public int queryAllUserCount(String username) {
PreparedStatement pstmt=null;
ResultSet rs=null;
String sql="select count(userid) from shop_user where 1=1 ";
StringBuffer sb=new StringBuffer();
sb.append(sql);
if(username!=null){
sb.append("and username like '%");
sb.append(username);
sb.append("%'");
}
sql=sb.toString();
int count=0;
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
// if(username!=null){
// pstmt.setString(1,username) ;
// }else{
// pstmt.setString(1,null);
// System.out.println(sql);
// }
rs=pstmt.executeQuery();
while(rs.next()){
count=rs.getInt(1);
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return count;
}
public int updatePassword(User user) {
int row=0;
String sql="update shop_user set password=? where username=?";
PreparedStatement pstmt=null;
int i=0;
try {
pstmt=ConnectionManager.getConnection().prepareStatement(sql);
pstmt.setString(++i,user.getPassword());
pstmt.setString(++i,user.getUsername());
row=pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return row;
}
我是从库里查的信息,库里只有一个USER表
|