📄 admindaoimpl.java
字号:
package org.ads123.goodsmanagers.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import org.JavaChina.myjdbc.ConnectPool;
import org.ads123.goodsmanagers.dao.AdminDao;
import org.ads123.goodsmanagers.dto.Admin;
public class AdminDaoImpl implements AdminDao {
public Admin findAdminByM_no(String m_no) {
Admin admin = null;
Connection conn = ConnectPool.getInstance().getConnection();
PreparedStatement pstm = null;
ResultSet rst = null;
String sql = "select M_pass, M_name, M_sex, M_age, M_mini from admin where M_no = ?";
try {
pstm = conn.prepareStatement(sql);
pstm.setString(1, m_no);
rst = pstm.executeQuery();
if (rst.next()) {
String m_pass = rst.getString("M_pass");
String m_name = rst.getString("M_name");
String m_sex = rst.getString("M_sex");
int m_age = rst.getInt("M_age");
String m_mini = rst.getString("M_mini");
admin = new Admin();
admin.setM_no(m_no);
admin.setM_pass(m_pass);
admin.setM_name(m_name);
admin.setM_sex(m_sex);
admin.setM_age(m_age);
admin.setM_mini(m_mini);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rst != null) {
rst.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null && !conn.isClosed()) {
ConnectPool.getInstance().destroyConnection(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return admin;
}
public ArrayList<Admin> findAdminAll() {
ArrayList<Admin> adminList = new ArrayList<Admin>();
Connection conn = ConnectPool.getInstance().getConnection();
PreparedStatement pstm = null;
ResultSet rst = null;
String sql = "select M_no, M_pass, M_name, M_sex, M_age, M_mini from admin";
try {
pstm = conn.prepareStatement(sql);
rst = pstm.executeQuery();
while (rst.next()) {
String m_no = rst.getString("M_no");
String m_pass = rst.getString("M_pass");
String m_name = rst.getString("M_name");
String m_sex = rst.getString("M_sex");
int m_age = rst.getInt("M_age");
String m_mini = rst.getString("M_mini");
adminList.add(new Admin(m_no, m_pass, m_name, m_sex, m_age,
m_mini));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rst != null) {
rst.close();
}
if (pstm != null) {
pstm.close();
}
if (conn != null && !conn.isClosed()) {
ConnectPool.getInstance().destroyConnection(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return adminList;
}
public void updateAdminInfo(Admin admin_update) {
Connection conn = ConnectPool.getInstance().getConnection();
PreparedStatement pstm = null;
String sql = "update admin set M_pass=?, M_name=?, M_sex=?, M_age=?, M_mini=? where M_no = ?";
try {
pstm = conn.prepareStatement(sql);
pstm.setString(1, admin_update.getM_pass());
pstm.setString(2, admin_update.getM_name());
pstm.setString(3, admin_update.getM_sex());
pstm.setInt(4, admin_update.getM_age());
pstm.setString(5, admin_update.getM_mini());
pstm.setString(6, admin_update.getM_no());
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstm != null){
pstm.close();
}
if(conn != null && !conn.isClosed()){
ConnectPool.getInstance().destroyConnection(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void addAdminInfo(Admin admin_add) {
Connection conn = ConnectPool.getInstance().getConnection();
PreparedStatement pstm = null;
String sql = "insert into admin values(?, ?, ?, ?, ?, ?)";
try {
pstm = conn.prepareStatement(sql);
pstm.setString(1, admin_add.getM_no());
pstm.setString(2, admin_add.getM_pass());
pstm.setString(3, admin_add.getM_name());
pstm.setString(4, admin_add.getM_sex());
pstm.setInt(5, admin_add.getM_age());
pstm.setString(6, admin_add.getM_mini());
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstm != null){
pstm.close();
}
if(conn != null && !conn.isClosed()){
ConnectPool.getInstance().destroyConnection(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public void deleteAdminInfo(String M_no) {
Connection conn = ConnectPool.getInstance().getConnection();
PreparedStatement pstm = null;
String sql = "delete from admin where M_no = ?";
try {
pstm = conn.prepareStatement(sql);
pstm.setString(1, M_no);
pstm.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(pstm != null){
pstm.close();
}
if(conn != null && !conn.isClosed()){
ConnectPool.getInstance().destroyConnection(conn);
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
AdminDaoImpl adminDaoImpl = new AdminDaoImpl();
Admin admin = adminDaoImpl.findAdminByM_no("ads");
if (admin != null) {
System.out.println(admin.getM_no() + " " + admin.getM_pass()
+ " " + admin.getM_name() + " " + admin.getM_sex()
+ " " + admin.getM_age() + " " + admin.getM_mini());
} else {
System.out.println("此管理员不存在!");
}
ArrayList<Admin> adminList = new ArrayList<Admin>();
adminList = adminDaoImpl.findAdminAll();
for (Admin list : adminList) {
System.out.println(list.getM_no() + " " + list.getM_pass() + " "
+ list.getM_name() + " " + list.getM_sex() + " "
+ list.getM_age() + " " + list.getM_mini());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -