1 package com.at221.jdbc; 2 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.sql.*; 6 import java.util.Properties; 7 8 import com.mysql.jdbc.Connection; 9 10 public class JDBCTools { 11 public static void release(Statement sta,java.sql.Connection con,ResultSet rs){ 12 if(sta != null){ 13 try{ 14 sta.close(); 15 }catch(Exception e){ 16 e.getStackTrace(); 17 } 18 } 19 20 if(con != null){ 21 try{ 22 con.close(); 23 }catch(Exception e){ 24 e.getStackTrace(); 25 } 26 } 27 28 if(rs != null){ 29 try { 30 rs.close(); 31 } catch (SQLException e) { 32 // TODO Auto-generated catch block 33 e.printStackTrace(); 34 } 35 } 36 37 } 38 39 public static void release(Statement sta,java.sql.Connection con){ 40 if(sta != null){ 41 try{ 42 sta.close(); 43 }catch(Exception e){ 44 e.getStackTrace(); 45 } 46 } 47 48 if(con != null){ 49 try{ 50 con.close(); 51 }catch(Exception e){ 52 e.getStackTrace(); 53 } 54 } 55 56 } 57 58 public static Connection getConnec() throws SQLException, IOException, ClassNotFoundException, 59 InstantiationException, IllegalAccessException{ 60 String driverClass = null; 61 String jdbcUrl = null; 62 String user = null; 63 String password = null; 64 65 //利用配置文件,来更改数据库的配置 66 InputStream is = JDBCTools.class.getClassLoader().getResourceAsStream("jdbc.properties"); 67 Properties p = new Properties(); 68 p.load(is); 69 driverClass = p.getProperty("driver"); 70 // System.out.println(driverClass); 71 72 jdbcUrl = p.getProperty("jdbcUrl"); 73 // System.out.println(jdbcUrl); 74 75 user = p.getProperty("user"); 76 // System.out.println(driverClass); 77 78 password = p.getProperty("password"); 79 // System.out.println(password); 80 81 Properties info = new Properties(); 82 info.put("user", user); 83 info.put("password", password); 84 DriverManager.registerDriver((Driver)Class.forName(driverClass).newInstance()); 85 86 Connection connec = (Connection)DriverManager.getConnection(jdbcUrl, user, password); 87 // System.out.println(connec); 88 return connec; 89 } 90 } 91 92 93 94 package com.at221.jdbc; 95 96 import java.sql.Connection; 97 import java.sql.PreparedStatement; 98 import java.sql.ResultSet; 99 import java.util.HashMap; 100 import java.util.Map; 101 102 import org.junit.Test; 103 104 import com.mysql.jdbc.ResultSetMetaData; 105 106 public class TestReflection { 107 @Test 108 public void test(){ 109 String sql = "SELECT id Id,name Name,birth Birth,email Email from student where id = ?"; 110 Student s = (Student)getStudent(Student.class,sql,3); 111 System.out.println(s); 112 } 113 public <T> Object getStudent(Class<T> clazz,String sql,Object ... args ){ 114 Connection conn = null; 115 PreparedStatement ps = null; 116 ResultSet rs = null; 117 ResultSetMetaData rsmd = null; 118 T entity = null; 119 try { 120 //1.SQL语句进行配置 121 conn = JDBCTools.getConnec(); 122 ps = conn.prepareStatement(sql); 123 //①给SQL语句中的通配符赋值 124 125 for(int i = 0; i < args.length; i++){ 126 ps.setObject(i+1, args[i]); 127 } 128 //②利用SQL进行查询得到结果集 129 rs = ps.executeQuery(); 130 //2.创建类的对象 131 entity = clazz.newInstance(); 132 //3.利用jdbc元数据来获得列的别名,为map中的键值对进行赋值 133 rsmd = (ResultSetMetaData)rs.getMetaData(); 134 135 Map<String, Object> value = new HashMap<String, Object>(); 136 if(rs.next()){ 137 for(int i = 0; i < rsmd.getColumnCount(); i++){ 138 String key = rsmd.getColumnLabel(i+1); 139 Object obj = rs.getObject(i+1); 140 value.put(key, obj); 141 } 142 } 143 //4.利用得到的map进行对创建的对象进行赋值 144 if(value.size() > 0){ 145 for(Map.Entry<String, Object> entry : value.entrySet()){ 146 String column = entry.getKey(); 147 Object columnValue = entry.getValue(); 148 RecflectionUtils.setFieldValue(entity, column, columnValue); 149 } 150 } 151 152 153 } catch(Exception e){ 154 e.printStackTrace(); 155 }finally { 156 JDBCTools.release(ps, conn); 157 158 } 159 return entity; 160 } 161 } 162 163 164 165 package com.at221.jdbc; 166 167 import java.sql.Date; 168 169 public class Student { 170 private int id; 171 private String name = null; 172 private Date birth = null; 173 private String email = null; 174 175 public Student(int id, String name, Date birth, String email) { 176 super(); 177 this.id = id; 178 this.name = name; 179 this.birth = birth; 180 this.email = email; 181 } 182 183 public Student() { 184 super(); 185 } 186 187 public int getId() { 188 return id; 189 } 190 191 public void setId(Integer id) { 192 this.id = id.intValue(); 193 } 194 195 public String getName() { 196 return name; 197 } 198 199 public void setName(String name) { 200 this.name = name; 201 } 202 203 public Date getBirth() { 204 return birth; 205 } 206 207 public void setBirth(Date birth) { 208 this.birth = birth; 209 } 210 211 public String getEmail() { 212 return email; 213 } 214 215 public void setEmail(String email) { 216 this.email = email; 217 } 218 219 @Override 220 public String toString() { 221 return "Student [id=" + id + ", name=" + name 222 + ", birth=" + birth + ", email=" + email + "]"; 223 } 224 225 226 }