Complete primary Universal DAO with Reflection

Source: Internet
Author: User

First, the goal

Complete primary Universal DAO with Reflection

Second, note

1.field[] fi = Clazz.getdeclaredfields ();

for (Field Ff:fi) {
Ff.setaccessible (TRUE);
Ff.set (ob, Rs.getobject (Ff.getname ()));
}

Third, the Code

Package Com.jikexueyuan.util;import Java.lang.reflect.field;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.util.arraylist;import Com.jikexueyuan.bean.antype;import Com.jikexueyuan.bean.animals;public class Basedao {public ArrayList getList (class CL) {ArrayList ar = new ArrayList (); Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; ResultSet rs = null; String sql = "SELECT * from" +cl.getsimplename (); Field[] fi = Cl.getdeclaredfields (); try {PS = conn.preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) { Object ob = Cl.newinstance ();//êµàý»¯àà¶ôïófor (Field ff:fi) {ff.setaccessible (true); Ff.set (ob, Rs.getobject ( Ff.getname ()));} Ar.add (OB);}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (Conn, PS, RS);} return ar;} Public Object Getobbyid (Class cl,int ID) {Object OB = NULL; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; ResultSet RS =null; Field[] fi = CL.getdeclaredfields (); String sql = "SELECT * from" +cl.getsimplename () + "where" +fi[0].getname () + "=" +id;try {PS = conn.preparestatement (SQL); r s = Ps.executequery (), while (Rs.next ()) {OB = Cl.newinstance (); for (Field Ff:fi) {ff.setaccessible (true); Ff.set (ob, Rs.getobject (Ff.getname ()));}}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (Conn, PS, RS);} return OB;} Public ArrayList Getlistbysome (Class cl, String name,object value) {ArrayList ar = new ArrayList (); Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; ResultSet rs = null; Field[] fi = Cl.getdeclaredfields ();  String sql = "SELECT * from" +cl.getsimplename () + "where" +name+ "=" "+value+" ' "; try {PS = conn.preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) {Object OB = Cl.newinstance (); for (Field Ff:fi) {ff.setaccessible (true); Ff.set (ob, Rs.getobject (Ff.getname ()));} Ar.add (OB);}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (Conn, PS, RS);} Return aR;} public boolean insert (Object ob) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Class cl = Ob.getclass (); Field[] fi = Cl.getdeclaredfields ();//insert into Animals (Name,age,anid) VALUES (?,?,?)  String sql = "INSERT INTO" +cl.getsimplename () + "("; for (int i = 1;i<fi.length;i++) {sql = Sql+fi[i].getname ();//4 0 1 2 3if (i!=fi.length-1) {sql = sql+ ",";}} sql = sql+ ") values ("; for (int i = 1;i<fi.length;i++) {sql = sql+ "?) "; if (i!=fi.length-1) {sql = sql+", ";}} sql = sql+ ")"; try {PS = conn.preparestatement (sql); for (int i = 1;i<fi.length;i++) {fi[i].setaccessible (true); Ps.setobject (i, Fi[i].get (OB));} int a = Ps.executeupdate (); if (a>0) {b = true;}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);} return b;} public boolean insert1 (Object ob) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Class cl = Ob.getclass (); Field[] fi = Cl.getdeclaredfields (); StringbuFfer sb = new StringBuffer ();//insert into Animals (Name,age,anid) VALUES (?,?,?) Sb.append ("INSERT INTO"), Sb.append (Cl.getsimplename ()), Sb.append ("("); for (int i = 1;i<fi.length;i++) {sb.append ( Fi[i].getname ()); if (i!=fi.length-1) {sb.append (",");}} Sb.append (") VALUES ("); for (int i = 1;i<fi.length;i++) {sb.append ("?"); if (i!=fi.length-1) {sb.append (",");}} Sb.append (")"), try {PS = conn.preparestatement (sb.tostring ()); for (int i = 1;i<fi.length;i++) {fi[i].setaccessible ( true);p S.setobject (i, Fi[i].get (OB));} int a = Ps.executeupdate (); if (a>0) {b = true;}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);} return b;} public boolean update (Object ob) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Class cl = Ob.getclass (); Field[] fi = Cl.getdeclaredfields (); StringBuffer sb = new StringBuffer ();//update animals Set name =?, age =?, Anid =? WHERE id =? sb.append ("Update"); Sb.append (Cl.getsimplename ()); Sb.append ("Set"), for (int i = 1;i<fi.length;i++) {fi[i].setaccessible (true); Sb.append (Fi[i].getname ()); Sb.append ("=?"); if (i!=fi.length-1) {sb.append (",");}} Sb.append ("where"); Sb.append (Fi[0].getname ()); Sb.append ("=?"); try {PS = conn.preparestatement (sb.tostring ()); for (int i = 1;i<fi.length;i++) {fi[i].setaccessible (true); Ps.setobject (i, Fi[i].get (OB));} Fi[0].setaccessible (True);p S.setobject (Fi.length, Fi[0].get (OB)), int a = Ps.executeupdate (), if (a>0) {b = true;}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);} return b;} public boolean Delete (Class cl, int id) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Field[] fi = Cl.getdeclaredfields (); String sql = "Delete from" +cl.getsimplename () + "where" +fi[0].getname () + "=?"; try {PS = conn.preparestatement (sql);p S.setobject (1, id), int a = Ps.executeupdate (), if (a>0) {b = true;}} catch (Excepti On e) {e.printstacktrace ();} Finally{baseconnectioN.closeres (conn, PS);} return b;} public boolean deletebysome (Class cl, String Name,object value) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Field[] fi = Cl.getdeclaredfields (); String sql = "Delete from" +cl.getsimplename () + "where" +name+ "=?"; try {PS = conn.preparestatement (sql);p S.setobject (1, value), int a = Ps.executeupdate (), if (a>0) {b = true;}} catch (Exce Ption e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);} return b;} public static void Main (string[] args) {Basedao bd = new Basedao (),//animals an = new Animals ();//an.setname ("½ð¾ååö");//a N.setage (//an.setanid) (1);//an.setid (3);//boolean B = bd.update (an); Bd.deletebysome (Animals.class, "name", " ½ð¾ååö ");//bd.delete (Animals.class, 5);//arraylist<animals> ar = bd.getlist (animals.class);//for (Animals an: AR) {//system.out.println ("±àºå£º" +an.getid () + "ãûxö£º" +an.getname () + "Äêáä:" +an.getage ());//}//arraylist< antype> arr = bd.getlist (Antype.class);for (Antype An:arr) {//system.out.println ("±àºå£º" +an.getanid () + "ãûxö£º" +an.getanname ()),//}//animals an = ( Animals) Bd.getobbyid (Animals.class, 2);//system.out.println (An.getname ());//arraylist<animals> AR = Bd.getlistbysome (Animals.class, "age", "a"),//for (Animals an:ar) {//system.out.println ("±àºå£º" +an.getid () + "ãûxö£ º "+an.getname () +" Äêáä: "+an.getage ())//}}}

Source code: http://files.cnblogs.com/files/shamgod/Java-DAO-4-v2.7z

Transfer from Jikexueyuan

Complete primary Universal DAO with Reflection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.