package JdbcDemo01;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
/**
* * 定义一个方法,查询emp表的数据将其封装为对象,然后装载集合,返回。
*/
public class JdbcDemo08 {
public static void main(String[] args) {
List<Emp> list = new JdbcDemo08().findAll();
System.out.println(list);
System.out.println(list.size());
for (Emp emp : list) {
System.out.println(emp);
}
}
public List<Emp> findAll(){
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
List<Emp> list = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql:///db1", "root", "root");
String sql = "select * from emp";
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
list = new ArrayList<Emp>();
while (rs.next()){
Emp emp = new Emp();
int id = rs.getInt(1);
String ename = rs.getString("ename");
int job_id = rs.getInt("job_id");
int mgr = rs.getInt("mgr");
Date joindate = rs.getDate("joindate");
double salary = rs.getDouble("salary");
double bonus = rs.getDouble("bonus");
int dept_id = rs.getInt("dept_id");
emp.setId(id);
emp.setName(ename);
emp.setJob_id(job_id);
emp.setMgr(mgr);
emp.setJoindate(joindate);
emp.setSalary(salary);
emp.setBonus(bonus);
emp.setDept_id(dept_id);
list.add(emp);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt!=null){
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
}
|
|