package cn.jdbc.Day5;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class map {
public static void main(String[] args) {
List<Job> a=fineAll();
for (Job job : a) {
System.out.println(job);
}
}
public static List<Job> fineAll(){
Connection conn=null;
Statement stat=null;
ResultSet rls=null;
List<Job> list=new ArrayList<>();
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/uul", "root", "root");
stat = conn.createStatement();
rls = stat.executeQuery("select * from job");
while(rls.next()){
Job job = new Job();
int id=rls.getInt("id");
String jname=rls.getString("jname");
String description=rls.getString("description");
job.setId(id);
job.setJname(jname);
job.setDescription(description);
list.add(job);
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
if (rls!=null){
try {
rls.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stat!=null){
try {
stat.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
return list;
}
}
|
|