📄 sqlserverlogindao.java~2~
字号:
package com.hope.itissue.sys_info.dao.sqlserver;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.ResultSet;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.hope.common.exception.BaseException;
import com.hope.common.util.db.DBHelper;
import com.hope.itissue.sys_info.dao.LoginDAO;
import com.hope.itissue.sys_info.bean.UserDTO;
public class SqlserverLoginDAO implements LoginDAO {
Log log = LogFactory.getLog(SqlserverLoginDAO.class);
public UserDTO getLoginDTO(String loginName, String password) throws
BaseException {
//定义Connection,PreparedStatement,ResultSet,LoginDTO等对象,并定义SQL语句
UserDTO userDTO = null;
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
String sql =
"select * from DN_Admin where LoginName=? and password=? and IsUse=?";
try {
//获取连接对象等等,并通过PreparedStatement对象对数据库进行查询,返回ResultSet对象
conn = DBHelper.getConnection();
pstm = conn.prepareStatement(sql);
pstm.setString(1, loginName);
pstm.setString(2, password);
pstm.setString(3, "1");
rs = pstm.executeQuery();
//如果结果集不为空,那么创建LoginDTO对象,并为其属性赋值
if (rs.next()) {
userDTO = new UserDTO();
userDTO.setID(rs.getString("ID"));
userDTO.setLoginName(rs.getString("LoginName"));
userDTO.setPassWord(rs.getString("PassWord"));
userDTO.setIsUse(rs.getString("IsUse"));
}
} catch (BaseException e) {
//设置记入日志的信息,并将异常封装为自定义异常抛出
log.error("error.OptDataBase.Error", e);
throw e;
} catch (Exception e1) {
//操作数据库失败。
log.error("error.OptDataBase.Error", e1);
throw new BaseException("error.OptDataBase.Error");
} finally {
//关闭连接
DBHelper.release(conn, pstm, rs);
}
return userDTO;
}
public boolean ifTruePass(String password) throws BaseException {
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
String sql = "select password from DN_Admin where LoginName='Admin'";
try {
//获取连接对象等等,并通过PreparedStatement对象对数据库进行查询,返回ResultSet对象
conn = DBHelper.getConnection();
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
//如果结果集不为空,那么创建LoginDTO对象,并为其属性赋值
if (rs.next()) {
if (rs.getString("password").equals(password)) {
return true;
} else {
return false;
}
} else {
return false;
}
} catch (BaseException e) {
//设置记入日志的信息,并将异常封装为自定义异常抛出
log.error("error.OptDataBase.Error", e);
throw e;
} catch (Exception e1) {
//操作数据库失败。
log.error("error.OptDataBase.Error", e1);
throw new BaseException("error.OptDataBase.Error");
} finally {
//关闭连接
DBHelper.release(conn, pstm, rs);
}
}
public void chgPassword(String newpass) throws BaseException{
Connection conn = null;
PreparedStatement pstm = null;
ResultSet rs = null;
try {
conn = DBHelper.getConnection();
String sql = "update DN_Admin set Password = '" + newpass
+ "' where LoginName='admin' ";
pstm = conn.prepareStatement(sql);
pstm.executeUpdate();
} catch (BaseException e) {
//设置记入日志的信息,并将异常封装为自定义异常抛出
log.error("error.OptDataBase.Error", e);
throw e;
} catch (SQLException e) {
log.error("error.OptDataBase.Error", e);
throw new BaseException("error.OptDataBase.Error");
}
finally {
//关闭连接
DBHelper.release(conn, pstm, rs);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -