📄 userpwddao.java
字号:
package dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import vo.UserPwdVo;
import common.jdbc.DbConnection;
import common.jdbc.DbException;
import common.jdbc.SqlConst;
/**
* 用户密码Dao
* @author linfeng
*
*/
public class UserPwdDao {
/**
* 获得用户密码
* @return vector
*/
public Vector getUserInfo() {
Vector vector = new Vector();
Connection con = null;
PreparedStatement stmt = null;
ResultSet set = null;
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.SELECT_USER_INFO);
set = stmt.executeQuery();
while (set.next()) {
int userId = set.getInt(1);
String userName = set.getString(2);
String userType = set.getString(3);
String userPassword = set.getString(4);
Long userPhone = set.getLong(5);
UserPwdVo value = new UserPwdVo(userId, userName, userType,
userPassword, userPhone);
vector.addElement(value);
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return vector;
}
/**
* 根据输入的用户名称和密码查找用户信息
* @param inputName 用户姓名
* @param inputPassword 密码
* @param inputType 用户类型
* @return flag
*/
public boolean findUser(String inputName, String inputPassword ,String inputType) {
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
ResultSet set = null;
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.SELECT_USER);
stmt.setString(1, inputName);
stmt.setString(2, inputPassword);
stmt.setString(3, inputType);
set = stmt.executeQuery();
// System.out.println(" not run to set.next");
if (set.next()) {
flag = true;
// System.out.println("run to set.next");
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return flag;
}
/**
* 查找数据库中是否存在某一编号的用户
* @param userId 用户编号
* @return flag
*/
public boolean findUserById(int userId) {
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
ResultSet set = null;
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.SELECT_USER_BY_ID);
stmt.setInt(1, userId);
set = stmt.executeQuery();
if (set.next()) {
flag = true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return flag;
}
/**
* 将输入的用户信息插入到数据库中
* @param value UserPwdVo对象
* @return flag
* @throws DbException 自定义异常
*/
public boolean insertUser(UserPwdVo value) throws DbException {
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
try {
if (findUserById(value.getUser_id())) {
throw new DbException("数据库中存在相同条件的记录");
}
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.INSERT_USER);
stmt.setInt(1, value.getUser_id());
stmt.setString(2, value.getUser_name());
stmt.setString(3, value.getUser_type());
stmt.setString(4, value.getPassword());
stmt.setLong(5, value.getPhone());
int count = stmt.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return flag;
}
/**
* 修改UserPwdVo表中的用户
* @param value UserPwdVo对象
* @return flag
*/
public boolean updateUser(UserPwdVo value) {
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.UPDATE_USER);
stmt.setInt(1, value.getUser_id());
stmt.setString(2, value.getUser_name());
stmt.setString(3, value.getUser_type());
stmt.setString(4, value.getPassword());
stmt.setLong(5, value.getPhone());
stmt.setInt(6, value.getUser_id());
int count = stmt.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return flag;
}
/**
* 根据输入的用户编号来删除用户
* @param userId 用户编号
* @return flag
*/
public boolean deleteUserById(int userId) {
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.DELETE_USER_BY_ID);
stmt.setInt(1, userId);
int count = stmt.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return flag;
}
/**
* 根据输入的用户姓名来删除用户
* @param userName 用户姓名
* @return flag
*/
public boolean deleteUserByName(String userName) {
Connection con = null;
PreparedStatement stmt = null;
boolean flag = false;
try {
con = new DbConnection().getConnection();
stmt = con.prepareStatement(SqlConst.DELETE_USER_BY_NAME);
stmt.setString(1, userName);
int count = stmt.executeUpdate();
if (count != 0) {
flag = true;
}
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return flag;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -