JDBC工具类:
1 package cn.Wuchang.zyDome; 2 3 import java.sql.*; 4 5 public class JDBCUtils { 6 private static final String r = "root"; 7 private static final String p = "root"; 8 private static final String url = "jdbc:mysql:///semployee"; 9 private static final String DRIVER = "com.mysql.jdbc.Driver"; 10 //注册驱动。 11 static{ 12 try { 13 Class.forName(DRIVER); 14 } catch (Exception e) { 15 e.printStackTrace(); 16 } 17 } 18 //得到数据库链接。 19 public static Connection getConnection() throws Exception { 20 return DriverManager.getConnection(url,r,p); 21 22 } 23 //关闭链接,执行打开的资源。 24 public static void close(Connection conn,Statement stmt){ 25 if (stmt!=null){ 26 try { 27 stmt.close(); 28 } catch (Exception e) { 29 e.printStackTrace(); 30 } 31 } 32 if (conn!=null){ 33 try { 34 conn.close(); 35 } catch (Exception e) { 36 e.printStackTrace(); 37 } 38 } 39 } 40 //关闭所有打开的资源。 41 public static void close(Connection conn, Statement stmt, ResultSet rs){ 42 if (stmt!=null){ 43 try { 44 stmt.close(); 45 } catch (Exception e) { 46 e.printStackTrace(); 47 } 48 }if (conn!=null){ 49 try { 50 conn.close(); 51 } catch (Exception e) { 52 e.printStackTrace(); 53 } 54 } 55 if (rs!=null){ 56 try { 57 rs.close(); 58 } catch (Exception e) { 59 e.printStackTrace(); 60 } 61 } 62 } 63 64 }
创建实体类:
1 package cn.Wuchang.zyDome; 2 3 import java.math.BigDecimal; 4 5 public class GetDome1 { 6 //创建变量。 7 private int uid; 8 private String uname; 9 private int Job_id; 10 private int mgr; 11 private String state; 12 private double salary; 13 private double bonus; 14 private int Dept_id; 15 16 //创建有参构造。 17 public GetDome1(int uid, String uname, int job_id, int mgr, String state, double salary, double bonus, int dept_id) { 18 this.uid = uid; 19 this.uname = uname; 20 Job_id = job_id; 21 this.mgr = mgr; 22 this.state = state; 23 this.salary = salary; 24 this.bonus = bonus; 25 Dept_id = dept_id; 26 } 27 //创建GetSet方法。 28 public int getUid() { 29 return uid; 30 } 31 32 public void setUid(int uid) { 33 this.uid = uid; 34 } 35 36 public String getUname() { 37 return uname; 38 } 39 40 public void setUname(String uname) { 41 this.uname = uname; 42 } 43 44 public int getJob_id() { 45 return Job_id; 46 } 47 48 public void setJob_id(int job_id) { 49 Job_id = job_id; 50 } 51 52 public int getMgr() { 53 return mgr; 54 } 55 56 public void setMgr(int mgr) { 57 this.mgr = mgr; 58 } 59 60 public String getState() { 61 return state; 62 } 63 64 public void setState(String state) { 65 this.state = state; 66 } 67 68 public double getSalary() { 69 return salary; 70 } 71 72 public void setSalary(double salary) { 73 this.salary = salary; 74 } 75 76 public double getBonus() { 77 return bonus; 78 } 79 80 public void setBonus(double bonus) { 81 this.bonus = bonus; 82 } 83 84 public int getDept_id() { 85 return Dept_id; 86 } 87 88 public void setDept_id(int dept_id) { 89 Dept_id = dept_id; 90 } 91 //创建toString方法。 92 @Override 93 public String toString() { 94 return "GetDome1{" + 95 "uid=" + uid + 96 ", uname='" + uname + '\'' + 97 ", Job_id=" + Job_id + 98 ", mgr=" + mgr + 99 ", state='" + state + '\'' + 100 ", salary=" + salary + 101 ", bonus=" + bonus + 102 ", Dept_id=" + Dept_id + 103 '}'; 104 } 105 }
把数据放到集合中并且查询出来:
1 package cn.Wuchang.zyDome; 2 3 import java.sql.*; 4 import java.util.*; 5 6 public class zyDome1 { 7 public static void main(String[] args) throws Exception { 8 fun1(); 9 } 10 public static void fun1(){ 11 12 List<GetDome1> list=new ArrayList<GetDome1>(); 13 Connection conn = null; 14 PreparedStatement ppst = null; 15 ResultSet rs = null; 16 try { 17 //获取数据库链接。 18 conn = JDBCUtils.getConnection(); 19 //获取对象执行SQL语句。 20 ppst = conn.prepareStatement("select * from emp"); 21 //执行查询。 22 rs = ppst.executeQuery(); 23 while (rs.next()){ 24 //添加对象。 25 list.add(new GetDome1( 26 rs.getInt("uid"), 27 rs.getString("uname"), 28 rs.getInt("Job_id"), 29 rs.getInt("mgr"), 30 rs.getString("state"), 31 rs.getDouble("salary"), 32 rs.getDouble("bonus"), 33 rs.getInt("Dept_id"))); 34 35 } 36 for(GetDome1 g:list){ 37 System.out.println(g.toString()); 38 } 39 40 41 } catch (Exception e) { 42 e.printStackTrace(); 43 } 44 finally { 45 JDBCUtils.close(conn,ppst,rs); 46 } 47 } 48 }