package com.market.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import com.market.entity.Employee;
import com.market.managerment.AdminManagerment;
import com.market.util.DataSourceTool;
public class EmployeeDao implements BaseDao<Employee, Integer> {
@Override
public boolean insert(Employee employee) {
Connection con = DataSourceTool.getConnection();
// TODO Auto-generated method stub
String sql = "insert into employee values(?,?,?,?,?,?);";
//通过链接来获取sql对象
PreparedStatement statement;
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setInt(1, employee.getEmp_no());
statement.setString(2, employee.getEmp_name());
statement.setString(3, employee.getEmp_password());
statement.setString(4, employee.getEmp_sex());
statement.setString(5, employee.getEmp_phone());
statement.setString(6, employee.getEmp_role());
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
return false;
}
@Override
public boolean delete(Integer key) {
Connection con = DataSourceTool.getConnection();
// TODO Auto-generated method stub
String sql = "delete from employee where emp_no=?";
//通过链接来获取sql对象
PreparedStatement statement;
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setInt(1, key);
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
return false;
}
@Override
public boolean update(Employee emp) {
Connection con = DataSourceTool.getConnection();
// TODO Auto-generated method stub
PreparedStatement statement;
//名字不为空
if (emp.getEmp_name()!=null) {
String sql = "update employee set emp_name=? where emp_no=?;";
//通过链接来获取sql对象
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setString(1, emp.getEmp_name());
statement.setInt(2, emp.getEmp_no());
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
}else if (emp.getEmp_password()!=null) {//密码不为空
String sql = "update employee set emp_password=? where emp_no=?;";
//通过链接来获取sql对象
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setString(1, emp.getEmp_password());
statement.setInt(2, emp.getEmp_no());
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
}else if (emp.getEmp_sex()!=null) {//性别不为空
String sql = "update employee set emp_sex=? where emp_no=?;";
//通过链接来获取sql对象
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setString(1, emp.getEmp_sex());
statement.setInt(2, emp.getEmp_no());
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
}else if (emp.getEmp_phone()!=null) {
String sql = "update employee set emp_phone=? where emp_no=?;";
//通过链接来获取sql对象
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setString(1, emp.getEmp_phone());
statement.setInt(2, emp.getEmp_no());
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
}else if (emp.getEmp_role()!=null) {
String sql = "update employee set emp_role=? where emp_no=?;";
//通过链接来获取sql对象
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//给占位符赋值
statement.setString(1, emp.getEmp_role());
statement.setInt(2, emp.getEmp_no());
//执行是不需要再提供sql语句
int result = statement.executeUpdate();
System.out.println(result > 0 ? "操作成功":"操作失败");
if (result > 0) {
return true;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
}
return false;
}
@Override
public List<Employee> query() {
Connection con = DataSourceTool.getConnection();
// TODO Auto-generated method stub
String sql = "select * from employee";
//通过链接来获取sql对象
PreparedStatement statement;
try {
//创建预编译sql语句对象
statement = con.prepareStatement(sql);
//执行是不需要再提供sql语句
ResultSet result = statement.executeQuery();
//从result遍历结果
while(result.next()) {
//获取
int emp_no = result.getInt("emp_no");
String emp_name = result.getString("emp_name");
String emp_password = result.getString("emp_password");
String emp_sex = result.getString("emp_sex");
String emp_phone = result.getString("emp_phone");
String emp_role = result.getString("emp_role");
System.out.printf("编号=%d \t 姓名=%s \t 密码=%s \t 性别=%s \t 手机号=%s 职务=%s \n",emp_no,emp_name,emp_password,emp_sex,emp_phone,emp_role);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
// 5、关闭链接
DataSourceTool.closeConnection(con);
}
return null;
}
@Override
public Employee queryByKey(Integer key) {
// TODO Auto-generated method stub
return null;
}
// 员工登录方法
public Employee queryLogin(Employee Employee) {
// 1、获取数据库链接
Connection con = DataSourceTool.getConnection();
// 2、定义sql语句
String sql = "select * from Employee where emp_name=? and emp_password=?;";
System.out.println(sql);
try {
// 3、获取sql语句对象
PreparedStatement statement = con.prepareStatement(sql);
statement.setString(1, Employee.getEmp_name());
statement.setString(2, Employee.getEmp_password());
// 4、执行sql查询语句
ResultSet result = statement.executeQuery();
while (result.next()) {
// 获取数据
int emp_no = result.getInt("emp_no");
String emp_name = result.getString("emp_name");
String emp_password = result.getString("emp_password");
String emp_sex = result.getString("emp_sex");
String emp_phone
没有合适的资源?快使用搜索试试~ 我知道了~
Market.rar
共76个文件
java:34个
class:34个
classpath:2个
需积分: 9 0 下载量 139 浏览量
2022-10-29
21:54:56
上传
评论
收藏 1.66MB RAR 举报
温馨提示
Market.rar
资源推荐
资源详情
资源评论
收起资源包目录
Market.rar (76个子文件)
Market
Market
bin
com
market
dao
MemberDao.class 3KB
BaseDao.class 466B
OrderDao.class 3KB
EmployeeDao.class 3KB
controller
ServerThread.class 4KB
Server.class 997B
DataPagecake.class 2KB
Client.class 3KB
entity
Order.class 669B
Member.class 3KB
Employee.class 2KB
util
DataSourceTool.class 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
com
market
dao
BaseDao.java 336B
OrderDao.java 4KB
EmployeeDao.java 2KB
MemberDao.java 2KB
controller
Server.java 668B
ServerThread.java 3KB
DataPagecake.java 1KB
Client.java 3KB
entity
Employee.java 2KB
Member.java 2KB
Order.java 272B
util
DataSourceTool.java 1KB
.project 382B
.classpath 375B
lib
mysql-connector-java-5.1.29.jar 856KB
bin
com
market
dao
MemberDao.class 4KB
BaseDao.class 500B
EmployeeDao.class 8KB
ClockDao.class 3KB
CarDao.class 2KB
ProductionDao.class 7KB
managerment
MenberManagerment.class 2KB
CashierManagerment.class 683B
AdminManagerment.class 5KB
controller
ServerThread.class 4KB
Server.class 997B
DataPagecake.class 2KB
Client.class 5KB
entity
Car.class 1KB
Clock.class 2KB
Member.class 3KB
Production.class 2KB
Employee.class 3KB
util
DataSourceTool.class 1KB
login
AdminLogin.class 2KB
CashierLogin.class 1KB
MenberLogin.class 1KB
.settings
org.eclipse.jdt.core.prefs 598B
src
com
market
dao
BaseDao.java 389B
CarDao.java 1KB
ProductionDao.java 7KB
EmployeeDao.java 10KB
MemberDao.java 3KB
ClockDao.java 3KB
managerment
CashierManagerment.java 313B
MenberManagerment.java 850B
AdminManagerment.java 5KB
controller
Server.java 668B
ServerThread.java 4KB
DataPagecake.java 1KB
Client.java 5KB
entity
Clock.java 2KB
Employee.java 2KB
Member.java 3KB
Car.java 768B
Production.java 1KB
util
DataSourceTool.java 1KB
login
AdminLogin.java 2KB
CashierLogin.java 703B
MenberLogin.java 677B
.project 382B
.classpath 375B
lib
mysql-connector-java-5.1.29.jar 856KB
共 76 条
- 1
资源评论
Outcast1105
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功