import java.sql.*;
public class Db {
private static Connection conn = null;
public static Connection getCon(){
try{
Class.forName("com.mysql.jdbc.Driver");
String user = "root";
String pwd = "root";
String url = "jdbc:mysql://localhost:3306/test";
conn = DriverManager.getConnection(url,user,pwd);
}catch(Exception e){
e.printStackTrace();
}
return conn;
}
}
/**
* 分页查询数据
* @param firstResult:上一页数据查询的起始位置
* @param pageSize:每页显示的数据行数
* @return
*/
public List<Employee> selectEmp(int firstResult,int pageSize){
Connection con = null;
List<Employee> list = new ArrayList<Employee>();
try{
con = Db.getCon();
String sql = "select * from tb_emp order by empId limit "+firstResult+","+pageSize+" ";
PreparedStatement stmt = con.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Employee emp = new Employee();
emp.setEmpId(rs.getInt("empId")); //设置编号
emp.setEmpName(rs.getString("empName")); //设置姓名
emp.setEmpSex(rs.getString("empSex")); //设置性别
emp.setEmpAge(rs.getInt("empAge")); //设置年龄
emp.setDuty(rs.getString("duty")); //设置职务
emp.setDept(rs.getString("dept")); //设置部门
emp.setTelephoneNo(rs.getString("telephoneNo"));//设置电话
emp.setAddress(rs.getString("address")); //设置联系地址
list.add(emp);
}
rs.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
con.close();
}catch(Exception e){
e.printStackTrace();
}
}
return list;
}