java使用Field实现通用数据库操作
上次介绍了Field,可以其可以在运行时候获取类中的信息,这是个好东西,我们可以用它来实现数据库的增删改查操作
当然,需要有一些限制:
1.表和实体类字段要对应
2.表和实体类名字要对应
3.实体类的第一个字段必须是主键(这个主要在更新的时候用到,具体根据个人情况而定)
数据库操作类如下
public class ReflectDao { private static String uri = "jdbc:mysql://localhost/reflect"; private static String password = "123"; private static String username = "xhe"; private String insert = "insert into "; private String update = "update "; private String delete = "delete from "; private String select = "select * from "; public Connection getConnection() { try { Class.forName("com.mysql.jdbc.Driver"); Connection conn = DriverManager.getConnection(uri, username, password); return conn; } catch (Exception e) { e.printStackTrace(); } return null; } public void save(Object obj, Class clazz) { Connection conn = null; Statement s = null; try { Field[] fields = clazz.getDeclaredFields(); insert += clazz.getSimpleName() + " values("; for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); if (fields[i].get(obj) != null) { if (i == 0) { insert = insert + "'" + fields[i].get(obj) + "'"; } else { insert = insert + ",'" + fields[i].get(obj) + "'"; } } else { if (i == 0) { insert = insert + "" + fields[i].get(obj) + ""; } else { insert = insert + "," + fields[i].get(obj) + ""; } } } insert += ")"; System.out.println(insert); conn = getConnection(); s = conn.createStatement(); s.execute(insert); } catch (Exception e) { e.printStackTrace(); } finally { try { if (s != null) s.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } public void update(Object obj, Class clazz) { Connection conn = null; Statement s = null; try { Field[] fields = clazz.getDeclaredFields(); update += clazz.getSimpleName() + " set "; for (int i = 0; i < fields.length; i++) { fields[i].setAccessible(true); if (fields[i].get(obj) != null) { if (i == 0) { update = update + fields[i].getName() + " = '" + fields[i].get(obj) + "'"; } else { update = update + "," + fields[i].getName() + " = '" + fields[i].get(obj) + "'"; } } } update += " where " + fields[0].getName() + " = " + fields[0].get(obj); System.out.println(update); conn = getConnection(); s = conn.createStatement(); s.executeUpdate(update); } catch (Exception e) { e.printStackTrace(); } finally { try { if (s != null) s.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } public List find(Object obj, Class clazz) { Connection conn = null; Statement s = null; ResultSet rs = null; List list = new ArrayList(); try { Field[] fields = clazz.getDeclaredFields(); select += clazz.getSimpleName() + " where 1 = 1"; for (int i = 0, length = fields.length; i < length; i++) { fields[i].setAccessible(true); if (fields[i].get(obj) != null) { select = select + " and " + fields[i].getName() + " = '" + fields[i].get(obj) + "'"; } } System.out.println(select); conn = getConnection(); s = conn.createStatement(); rs = s.executeQuery(select); while (rs.next()) { Object o = clazz.newInstance(); for (int i = 0, length = fields.length; i < length; i++) { fields[i].setAccessible(true); fields[i].set(o, rs.getObject(i + 1)); } list.add(o); } } catch (Exception e) { e.printStackTrace(); } finally { try { if (s != null) s.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } return list; } public void delete(Object obj, Class clazz) { Connection conn = null; Statement s = null; ResultSet rs = null; try { Field[] fields = clazz.getDeclaredFields(); delete += clazz.getSimpleName() + " where 1 = 1"; for (int i = 0, length = fields.length; i < length; i++) { fields[i].setAccessible(true); if (fields[i].get(obj) != null) { delete = delete + " and " + fields[i].getName() + " = '" + fields[i].get(obj) + "'"; } } System.out.println(delete); conn = getConnection(); s = conn.createStatement(); s.executeUpdate(delete); } catch (Exception e) { e.printStackTrace(); } finally { try { if (s != null) s.close(); if (conn != null) conn.close(); } catch (Exception e) { e.printStackTrace(); } } } }这个类很简单,外部调用的时候只需要传入对应的对象和class对象,就可以进行增删改查操作
测试类如下
public static void main(String[] args) { ReflectDao reflect = new ReflectDao(); Student s = new Student(); s.setAge(12); s.setName("tom"); s.setSex("man"); s.setStudentid(1130299110); reflect.save(s, s.getClass()); }这样就可以把Student的信息保存到数据库的Student表了(表就4个字段)
不得不说Field是一个好东西,不过这里主要是大概的提供一种应用,所以上面的程序会有BUG或者不完美的地方,各位可以使用Field去写一个自己的操作方法
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。