📄 admindao.java
字号:
package com.qrsx.shop.dao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.qrsx.shop.model.Admin;
/**
*@Author:李世海
*@Address:青岛软件园
*@Date: Mar 31, 2009
*/
public class AdminDAO extends BaseDAO{
/**
* 判断登陆信息是否正确
* @return
* @throws SQLException
*/
public Admin login(Admin admin) throws SQLException{
String sql = "select * from admin where name=? and password=?";
ps = conn.prepareStatement(sql);
ps.setString(1, admin.getName());
ps.setString(2, admin.getPassword());
ResultSet rs = ps.executeQuery();
Admin a = null;
while(rs.next()){
a= new Admin();
a.setId(Integer.valueOf(rs.getInt("id")));
a.setName(rs.getString("name"));
a.setPassword(rs.getString("password"));
}
return a;
}
/**
* 修改密码
* @throws SQLException
*/
public void update(Admin admin,String newPassword) throws SQLException{
String sql = "update admin set password=? where id=?";
ps = conn.prepareStatement(sql);
ps.setString(1, newPassword);
ps.setInt(2, admin.getId());
ps.executeUpdate();
}
/**
* 创建管理员
* @throws SQLException
*/
public void create(Admin admin) throws SQLException{
String sql = "insert into admin (name,password) values(?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, admin.getName());
ps.setString(2, admin.getPassword());
ps.executeUpdate();
}
/**
* 查询所有管理员
* @throws SQLException
*/
public List<Admin> findAll() throws SQLException{
String sql = "select * from admin";
ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
List<Admin> list= new ArrayList<Admin>();
while(rs.next()){
Admin admin = new Admin();
admin.setId(rs.getInt("id"));
admin.setName(rs.getString("name"));
admin.setPassword(rs.getString("password"));
list.add(admin);
}
return list;
}
/**
* 删除管理员
* @throws SQLException
*/
public void delete(Integer id) throws SQLException{
String sql = "delete from admin where id=?";
ps =conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -