|
|
|
@ -4,81 +4,19 @@ import com.msb.bean.Emp;
|
|
|
|
|
import com.msb.dao.BaseDao;
|
|
|
|
|
import com.msb.dao.EmpDao;
|
|
|
|
|
|
|
|
|
|
import java.sql.*;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class EmpDaoImp extends BaseDao implements EmpDao {
|
|
|
|
|
public static String driver = "com.mysql.cj.jdbc.Driver";
|
|
|
|
|
public static String url = "jdbc:mysql://127.0.0.1:3306/mydb?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai";
|
|
|
|
|
public static String user = "root";
|
|
|
|
|
public static String password = "Fnst1234";
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public List<Emp> findAll() {
|
|
|
|
|
Connection connection = null;
|
|
|
|
|
|
|
|
|
|
List<Emp> list =null;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 选择数据库
|
|
|
|
|
Class.forName(driver);
|
|
|
|
|
|
|
|
|
|
// 建立连接
|
|
|
|
|
connection = DriverManager.getConnection(url, user, password);
|
|
|
|
|
|
|
|
|
|
// 执行sql
|
|
|
|
|
String sql = "select * from emp";
|
|
|
|
|
list = baseQuery(connection, Emp.class, sql);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
try {
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return list;
|
|
|
|
|
String sql = "select * from emp";
|
|
|
|
|
return baseQuery(Emp.class, sql);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int deleteByEmpno(int empno) {
|
|
|
|
|
Connection connection = null;
|
|
|
|
|
|
|
|
|
|
int rows = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 选择数据库
|
|
|
|
|
Class.forName(driver);
|
|
|
|
|
|
|
|
|
|
// 建立连接
|
|
|
|
|
connection = DriverManager.getConnection(url, user, password);
|
|
|
|
|
|
|
|
|
|
// 执行sql
|
|
|
|
|
String sql = "delete from emp where empno = ?";
|
|
|
|
|
rows = baseUpdate(connection, sql, empno);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
try {
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rows;
|
|
|
|
|
String sql = "delete from emp where empno = ?";
|
|
|
|
|
return baseUpdate(sql, empno);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
@ -88,34 +26,7 @@ public class EmpDaoImp extends BaseDao implements EmpDao {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public int addEmp(Emp emp) {
|
|
|
|
|
Connection connection = null;
|
|
|
|
|
|
|
|
|
|
int rows = 0;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// 选择数据库
|
|
|
|
|
Class.forName(driver);
|
|
|
|
|
|
|
|
|
|
// 建立连接
|
|
|
|
|
connection = DriverManager.getConnection(url, user, password);
|
|
|
|
|
|
|
|
|
|
// 执行sql
|
|
|
|
|
String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
|
|
|
|
|
rows = baseUpdate(connection, sql, emp.getEmpno(), emp.getEname(), emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno());
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
finally {
|
|
|
|
|
try {
|
|
|
|
|
if (connection != null) {
|
|
|
|
|
connection.close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (SQLException e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return rows;
|
|
|
|
|
String sql = "insert into emp values(?,?,?,?,?,?,?,?)";
|
|
|
|
|
return baseUpdate(sql, emp.getEmpno(), emp.getEname(), emp.getJob(),emp.getMgr(),emp.getHiredate(),emp.getSal(),emp.getComm(),emp.getDeptno());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|