📄 mysqluserfactory.java
字号:
package net.ijsp.news.news.mysql;
/**
* Title 用户相关信息
* @author: ccjsmile
* Company: http://www.ijsp.net
* Copyright: Copyright (c) 2003
* @version 1.0 beta
*/
import net.ijsp.news.news.UserFactory;
import net.ijsp.news.news.User;
import net.ijsp.news.news.UserNotFoundException;
import net.ijsp.news.news.UserAlreadExistsException;
import net.ijsp.news.news.UserPasswordException;
import net.ijsp.news.database.DBConnect;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MysqlUserFactory extends UserFactory {
private ResultSet rs ;
/**
* 验证用户密码
*/
public User chkUser(User user) throws UserNotFoundException,UserPasswordException {
DBConnect dbc = null;
try{
dbc = new DBConnect();
dbc.prepareStatement("SELECT password,flag FROM admin WHERE username=?");
dbc.setString(1,user.getUsername());
rs = dbc.executeQuery();
if(rs.next()) {
if(user.getPassword().equals(rs.getString(1)))
user.setFlag(rs.getInt(2));
else
throw new UserPasswordException();
}else
throw new UserNotFoundException();
}catch(SQLException e){
System.err.println(e);
}finally{
try{
if(dbc!=null)
dbc.close();
}catch(Exception e){
}
}
return user;
}
/**
* 添加编辑人员
*/
public void add(User user) throws UserAlreadExistsException {
DBConnect dbc1 = null;
boolean exists = true;
try{
try{
chkUser(user);
}catch(UserNotFoundException u){
exists = false;
}catch(Exception e){
}
if(exists){
throw new UserAlreadExistsException();
}else{
dbc1 = new DBConnect();
System.out.println(dbc1);
dbc1.prepareStatement("INSERT INTO admin (username,password,flag) VALUES (?,?,?)");
dbc1.setString(1,user.getUsername());
dbc1.setString(2,user.getPassword());
dbc1.setInt(3,user.getFlag());
dbc1.executeUpdate();
}
}catch(SQLException e){
System.err.println(e);
}finally{
try{
if(dbc1!=null)
dbc1.close();
}catch(Exception e){
}
}
}
/**
* 修改密码
*/
public void reSetPassword(User user) {
DBConnect dbc = null;
try{
dbc = new DBConnect();
dbc.prepareStatement("UPDATE admin SET password=? WHERE username=?");
dbc.setString(1,user.getPassword());
dbc.setString(2,user.getUsername());
dbc.executeUpdate();
}catch(SQLException e){
System.err.println(e);
}finally{
try{
if(dbc!=null)
dbc.close();
}catch(Exception e){
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -