一。以下为增删改查,分页,模糊查询的dao层实现
package servlet02.dao.impl;
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 jdbc.util.DBConnection;
import servlet02.Model.Worker;
import servlet02.dao.intf.WorkerDao;
public class WorkerDaoImpl implements WorkerDao{
//数据添加
public void add(Worker worker) {
Connection connection = DBConnection.getConnection();
String sql = "INSERT INTO office(username,age,sex,department,headerUrl,password) VALUES(?,?,?,?,?,?)";
PreparedStatement ps = null;
try {
ps = connection.prepareStatement(sql);
ps.setString(1, worker.getUsername());
ps.setInt(2, worker.getAge());
ps.setInt(3, worker.getSex());
ps.setString(4, worker.getDepartment());
ps.setString(5, worker.getHeaderUrl());
ps.setString(6, worker.getPassword());
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBConnection.close(ps, null, connection);
}
}
@Override
//数据查询
public List<Worker> query() {
Connection connection = DBConnection.getConnection();
List<Worker> workers = new ArrayList<Worker>();
String sql = "SELECT id,username,age,sex,department,headerUrl,password FROM office";
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = connection.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
String id=rs.getString("id");
String username = rs.getString("username");
String age = rs.getString("age");
String sex = rs.getString("sex");
String department =rs.getString("department");
String headerUrl = rs.getString("headerUrl");
String password = rs.getString("password");
Worker worker = new Worker(Integer.parseInt(id),username,Integer.parseInt(age),Integer.parseInt(sex), department, headerUrl,password );
workers.add(worker);
}
ps.execute();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
DBConnection.close(ps, rs, connection);
}
return workers;
}
//数据修改 username,age,sex,department,headerUrl,password
public void update(Worker worker) {
Connection connection=DBConnection.getConnection();