📄 logindao.java
字号:
package com.wuliu.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import com.wuliu.DBConnection.DBConnection;
import com.wuliu.entity.Login;
public class LoginDAO {
/**
* @袁子鹏
* 登录信息表.DAO
*/
private DBConnection dao = null;
private Connection cn = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
/*
*
* 模糊查询所有的收货人
*
*/
public List<Login> selectLoginMsg(String loginId)
{
this.dao = new DBConnection();
this.cn = this.dao.getConnection();
List<Login> list = new ArrayList<Login>();
Login login = null;
try
{
this.ps = this.cn.prepareStatement("select * from LoginTable where LoginId like ?");
ps.setString(1,"%"+loginId+"%");
ResultSet rs = this.ps.executeQuery();
while (rs.next())
{
int LoginTableId = rs.getInt("LoginTableId");
String LoginId = rs.getString("LoginId");
String LoginPassword = rs.getString("LoginPassword");
String LoginPower = rs.getString("LoginPower");
String EmployeeId = rs.getString("EmployeeId");
login = new Login(LoginTableId, LoginId,
LoginPassword, LoginPower, EmployeeId);
list.add(login);
}
} catch (SQLException e)
{
e.printStackTrace();
}
this.dao.closeConnection(cn);
return list;
}
//查询一个管理员(2.13崔斌添加)
public Login selectAdminById(String loginId){
Login login = null;
this.dao = new DBConnection();
this.cn = this.dao.getConnection();
String sql = "select * from LoginTable where LoginId='"+loginId+"'";
try {
this.ps = this.cn.prepareStatement(sql);
rs = this.ps.executeQuery();
if (rs.next()) {
int LoginTableId = rs.getInt("LoginTableId");
String LoginId = rs.getString("LoginId");
String LoginPassword = rs.getString("LoginPassword");
String LoginPower = rs.getString("LoginPower");
String EmployeeId = rs.getString("EmployeeId");
login = new Login(LoginTableId, LoginId,
LoginPassword, LoginPower, EmployeeId);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
this.dao.closeResultSet(rs);
this.dao.closeConnection(cn);
}
return login;
}
// 查看登陆表中的所有内容
public List<Login> selectAllLogin() {
this.dao = new DBConnection();
this.cn = this.dao.getConnection();
List<Login> list = new ArrayList<Login>();
Login log = null;
String sql = "select * from LoginTable";
try {
this.ps = this.cn.prepareStatement(sql);
rs = this.ps.executeQuery();
while (rs.next()) {
int LoginTableId = rs.getInt("LoginTableId");
String LoginId = rs.getString("LoginId");
String LoginPassword = rs.getString("LoginPassword");
String LoginPower = rs.getString("LoginPower");
String EmployeeId = rs.getString("EmployeeId");
log = new Login(LoginTableId, LoginId,
LoginPassword, LoginPower, EmployeeId);
list.add(log);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
this.dao.closeResultSet(rs);
this.dao.closeConnection(cn);
}
return list;
}
//新增一个管理员
public void insertLogin(Login log){
this.dao = new DBConnection();
this.cn = this.dao.getConnection();
try
{
PreparedStatement ps = cn.prepareStatement("insert into " +
"LoginTable(LoginId,LoginPassword,LoginPower,EmployeeId) " +
"values(?,?,?,?)");
ps.setString(1,log.getLoginId());
ps.setString(2,log.getLoginPassword());
ps.setString(3,log.getLoginPower());
ps.setString(4, log.getEmployeeId());
ps.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
}finally{
this.dao.closeConnection(cn);
}
}
//根据 LoginId 删除一个管理员
public void deleteLogin(String LoginId){
this.dao = new DBConnection();
this.cn = this.dao.getConnection();
try
{
Statement st = cn.createStatement();
st.executeUpdate("delete from LoginTable where LoginId='"+LoginId+"'");
} catch (SQLException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.dao.closeConnection(cn);
}
}
//修改一个管理员的信息
public boolean updateLogin(String LoginId,String LoginPassword,String LoginPower,String EmployeeId){
this.dao = new DBConnection();
this.cn = this.dao.getConnection();
boolean flag = true;
try
{
PreparedStatement ps = cn.prepareStatement("update LoginTable set LoginPassword=? ,LoginPower=?,EmployeeId=? where LoginId=?");
ps.setString(1,LoginPassword);
ps.setString(2,LoginPower);
ps.setString(3, EmployeeId);
ps.setString(4,LoginId);
ps.addBatch();
ps.executeUpdate();
} catch (SQLException e)
{
e.printStackTrace();
flag = false;
}finally{
this.dao.closeConnection(cn);
}
return flag;
}
public static void main(String[] args){
// List<Login> list =new ArrayList<Login>();
// LoginDAO pd =new LoginDAO();
// Login login = pd.selectAdminById("L0001");
// if(login==null){
// System.out.println("kong");
// }else{
// System.out.println(login.getLoginPassword());
// System.out.println(login.getLoginPower());
// System.out.println(login.getLoginTableId());
// System.out.println(login.getEmployeeId());}
// list=pd.selectLoginMsg("l");
// for(int i=0;i<list.size();i++){
// System.out
// .println(list.get(i).getLoginTableId()+list.get(i).getLoginId()
// +list.get(i).getLoginPassword()+list.get(i).getLoginPower()+list.get(i).getEmployeeId());
// }
//测试新增管理员
// Login log = new Login("L0001","123456","B","EP0902");
// LoginDAO ldao = new LoginDAO();
// ldao.insertLogin(log);
//删除一个管理员
// ldao.deleteLogin("L0001");
//修改一个管理员的信息
// ldao.updateLogin("L0000", "123456", "A", "EP0903");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -