package org.lanqiao.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.lanqiao.entity.Employe;
import org.lanqiao.util.DataUtil;
import org.lanqiao.util.DataUtil;
public class EmployeDao {
// 增加
public void insert(Employe e) {
// 获取连接
Connection cn = DataUtil.getConnection();
// 增加
public void insert(Employe e) {
// 获取连接
Connection cn = DataUtil.getConnection();
// 准备sql
String sql = "insert into yuangong values(null,?,?,?,?,?,?)";
// 获取preparedstatement
try {
PreparedStatement pa = cn.prepareStatement(sql);
// 填充占位符
pa.setString(1, e.getName());
pa.setString(2, e.getPwd());
pa.setInt(3, e.getGender());
pa.setObject(4, e.getBirth());
pa.setString(5, e.getIphone());
pa.setInt(6, e.getRole());
// 执行sql
pa.executeUpdate();
cn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
String sql = "insert into yuangong values(null,?,?,?,?,?,?)";
// 获取preparedstatement
try {
PreparedStatement pa = cn.prepareStatement(sql);
// 填充占位符
pa.setString(1, e.getName());
pa.setString(2, e.getPwd());
pa.setInt(3, e.getGender());
pa.setObject(4, e.getBirth());
pa.setString(5, e.getIphone());
pa.setInt(6, e.getRole());
// 执行sql
pa.executeUpdate();
cn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
// 修改
public void update(Employe e) {
Connection cn = DataUtil.getConnection();
String sql = "update yuangong set name=?,gender=?,birth=?,tel=?,role=? where id=?";
try {
PreparedStatement pa = cn.prepareStatement(sql);
pa.setString(1, e.getName());
pa.setInt(2, e.getGender());
pa.setObject(3, e.getBirth());
pa.setString(4, e.getIphone());
pa.setInt(5, e.getRole());
pa.setInt(6, e.getId());
pa.executeUpdate();
cn.close();
public void update(Employe e) {
Connection cn = DataUtil.getConnection();
String sql = "update yuangong set name=?,gender=?,birth=?,tel=?,role=? where id=?";
try {
PreparedStatement pa = cn.prepareStatement(sql);
pa.setString(1, e.getName());
pa.setInt(2, e.getGender());
pa.setObject(3, e.getBirth());
pa.setString(4, e.getIphone());
pa.setInt(5, e.getRole());
pa.setInt(6, e.getId());
pa.executeUpdate();
cn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
e1.printStackTrace();
}
}
// 删除
public void delete(int id) {
Connection cn = DataUtil.getConnection();
String sql = "delete from yuangong where id=?";
try {
PreparedStatement pa = cn.prepareStatement(sql);
pa.setInt(1, id);
// 执行sql
pa.executeUpdate();
// 关闭连接池
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
public void delete(int id) {
Connection cn = DataUtil.getConnection();
String sql = "delete from yuangong where id=?";
try {
PreparedStatement pa = cn.prepareStatement(sql);
pa.setInt(1, id);
// 执行sql
pa.executeUpdate();
// 关闭连接池
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<Employe> getAll() {
List<Employe> em = new ArrayList();
Connection cn = DataUtil.getConnection();
String sql = "select * from yuangong";
try {
PreparedStatement pa = cn.prepareStatement(sql);
ResultSet re = pa.executeQuery();
// 全部读取 如果还有下一位
// 读取就是反过来
while (re.next()) {
Employe c = new Employe();
c.setId(re.getInt(1));
c.setName(re.getString(2));
// c.setPwd(re.getString(3));
c.setGender(re.getInt(4));
c.setBirth(DataUtil.toLocalDate(re.getDate(5)));
c.setIphone(re.getString(6));
c.setRole(re.getInt(7));
em.add(c);
}
cn.close();
return em;
} catch (SQLException e1) {
e1.printStackTrace();
}
return null;
}
List<Employe> em = new ArrayList();
Connection cn = DataUtil.getConnection();
String sql = "select * from yuangong";
try {
PreparedStatement pa = cn.prepareStatement(sql);
ResultSet re = pa.executeQuery();
// 全部读取 如果还有下一位
// 读取就是反过来
while (re.next()) {
Employe c = new Employe();
c.setId(re.getInt(1));
c.setName(re.getString(2));
// c.setPwd(re.getString(3));
c.setGender(re.getInt(4));
c.setBirth(DataUtil.toLocalDate(re.getDate(5)));
c.setIphone(re.getString(6));
c.setRole(re.getInt(7));
em.add(c);
}
cn.close();
return em;
} catch (SQLException e1) {
e1.printStackTrace();
}
return null;
}
public Employe getEmploye(int id, String password) {
Connection cn = DataUtil.getConnection();
String sql = "select * from yuangong where id=? and password=?";
try {
PreparedStatement ps = cn.prepareStatement(sql);
ps.setInt(1, id);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Employe e = new Employe();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPwd(rs.getString(3));
e.setGender(rs.getInt(4));
e.setBirth(DataUtil.toLocalDate(rs.getDate(5)));
e.setIphone(rs.getString(6));
e.setRole(rs.getInt(7));
Connection cn = DataUtil.getConnection();
String sql = "select * from yuangong where id=? and password=?";
try {
PreparedStatement ps = cn.prepareStatement(sql);
ps.setInt(1, id);
ps.setString(2, password);
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Employe e = new Employe();
e.setId(rs.getInt(1));
e.setName(rs.getString(2));
e.setPwd(rs.getString(3));
e.setGender(rs.getInt(4));
e.setBirth(DataUtil.toLocalDate(rs.getDate(5)));
e.setIphone(rs.getString(6));
e.setRole(rs.getInt(7));
return e;
}
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
public void updatePwd(Employe em) {
Connection cn = DataUtil.getConnection();
String sql = "update yuangong set password=? where id=?";
try {
PreparedStatement pa = cn.prepareStatement(sql);
pa.setString(1, em.getPwd());
pa.setInt(2,em.getId());
pa.executeUpdate();
cn.close();
Connection cn = DataUtil.getConnection();
String sql = "update yuangong set password=? where id=?";
try {
PreparedStatement pa = cn.prepareStatement(sql);
pa.setString(1, em.getPwd());
pa.setInt(2,em.getId());
pa.executeUpdate();
cn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
}
e1.printStackTrace();
}
}
//人員增加刪除
//是可以使用優化的 寫一個通用方法 然後節省代碼量
package org.lanqiao.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.lanqiao.until.DataUtil;
public class BaseDao {
//增加删除修改
public void CurrencyDML(String sql, Object... args) throws SQLException {
Connection cn = new DataUtil().getConnection();
PreparedStatement ps = cn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
ps.executeUpdate();
cn.close();
}
//进行查询操作
//增加删除修改
public void CurrencyDML(String sql, Object... args) throws SQLException {
Connection cn = new DataUtil().getConnection();
PreparedStatement ps = cn.prepareStatement(sql);
for (int i = 0; i < args.length; i++) {
ps.setObject(i+1, args[i]);
}
ps.executeUpdate();
cn.close();
}
//进行查询操作
}
//下面的是用通用過後節省的代碼樣式
package org.lanqiao.dao;
import java.sql.SQLException;
import org.lanqiao.entity.Word;
public class WordDao extends BaseDao{
//添加
public void insertWord(Word w){
String sql="insert into words(name,chinese,Rootofword,rootofwordchinese) values(?,?,?,?)";
try {
CurrencyDML(sql,w.getName(),w.getChinese(),w.getRootofword(),w.getRootofwordchinese());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//删除
public void deleteWord(int id){
String sql="delete from words where id=?";
try {
CurrencyDML(sql, id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//修改
public void updataWord(Word w){
String sql="update words set name=?,chinese=?,Rootofword=?,rootofwordchinese=? where id=?";
try {
CurrencyDML(sql,w.getName(),w.getChinese(),w.getRootofword(),w.getRootofwordchinese(),w.getId());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}