The universal JDBC Tool class. Simple processing of database operations directly through reflection mechanism

Source: Internet
Author: User
Tags dba stringbuffer

By learning from the experience of other great gods to study this set of code, thank those who share the knowledge of the great God, so I will be the content of their own to contribute to the common progress. Thank you.
These two classes, can be done directly after generating a jar file, so the next project directly into the jar file can be called.
Myjdbc.properties This file is placed under the SRC file of the project file, the contents of the configuration are as follows:
Conurl=jdbc:sqlserver://192.168.1.1:1433;databasename= Database name Cname= com.microsoft.sqlserver.jdbc.sqlserverdriverdba= Login name dbpassword= password
The other database is the same configuration, only need to change the configuration information above the line.
Package com. Yy.util;import Java.io.ioexception;import Java.io.inputstream;import Java.sql.connection;import Java.sql.drivermanager;import Java.sql.preparedstatement;import Java.sql.resultset;import java.sql.SQLException; Import Java.sql.statement;import Java.util.properties;public class Baseconnection {private static String Conurl = ""; private static string cname = "";p rivate static string DbA = "";p rivate static string dbpassword = ""; static{try {//Get External configuration The database link information inputstream IPs =baseconnection.class.getclassloader (). getResourceAsStream ("myjdbc.properties"); Properties Props = new properties (); try {props.load (IPS);} catch (IOException e) {//TODO auto-generated catch Blocke.prin Tstacktrace ();} try {ips.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} Conurl = Props.getproperty ("Conurl"); CNAME =props.getproperty ("CNAME");d ba = Props.getproperty ("DbA");d bpassword = Props.getproperty ("Dbpassword");} catch (Exception e) {//TODO auto-generated catch blocKe.printstacktrace ();}}        public static Connection getconnection () {Connection conn = null;            try {class.forname (CNAME);        conn = Drivermanager.getconnection (Conurl, DbA, Dbpassword);        } catch (Exception e) {e.printstacktrace ();    } return conn; }public static void Closeres (Connection conn, preparedstatement PS) {try {if (conn! = null) {Conn.close ();} if (PS! = null) {Ps.close ();}} catch (Exception e) {e.printstacktrace ();}} public static void Closeres (Connection conn, PreparedStatement Ps,resultset rs) {try {if (conn! = null) {Conn.close ()} if (PS! = null) {Ps.close ();} if (rs! = null) {Rs.close ();}} catch (Exception e) {e.printstacktrace ();}}} Package com. Yy.util;import Java.lang.reflect.field;import Java.sql.connection;import Java.sql.preparedstatement;import Java.sql.resultset;import Java.util.arraylist;public class Basedao {//universal access to all content public static ArrayList GetList (class C1) {ArrayList ar = new ArrayList (); COnnection conn = Baseconnection.getconnection (); PreparedStatement PS = null; ResultSet rs = null; String sql = "SELECT * from" + c1.getsimplename (); Field[] fi = C1.getdeclaredfields (); try{ps = conn.preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) { Object OB = C1.newinstance ();//Instantiate Class object 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;} Universal gets the ID of the primary key for a single piece of data. public static Object Getobbyid (Class c1,int ID) {Object OB = NULL; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; ResultSet rs = null; Field[] fi = C1.getdeclaredfields (); String sql = "SELECT * from" + c1.getsimplename () + "where" + fi[0].getname () + "=" +id;try{ps = conn.preparestatement (sql); rs = Ps.executequery (), while (Rs.next ()) {OB = C1.newinstance ();//Instantiate Class object 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;} Omnipotent gets the data after the where condition. public static ArrayList getList (Class c1,string wheresql) {ArrayList ar = new ArrayList (); Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; ResultSet rs = null; String sql = "SELECT * from" + c1.getsimplename () + "" + wheresql; Field[] fi = C1.getdeclaredfields (); try{ps = conn.preparestatement (sql); rs = Ps.executequery (); while (Rs.next ()) { Object OB = C1.newinstance ();//Instantiate Class object 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;} The universal method of insertion. public static Boolean Insert (Object ob) {Boolean f = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Class C1 = Ob.getclass (); Field[] fi = C1.getdeclaredfields (); StringBuffer Sql = new StringBuffer (); StringBuffer SQL1 = new StringBuffer (); Sql.append ("INSERT into"). APpend (C1.getsimplename ()). Append ("("); for (int i =1; i<fi.length;i++) {fi[i].setaccessible (true); Sql.append (Fi[i].getname ()); Sql1.append ("?"); if (i!= fi.length-1) {sql.append (","); Sql1.append (",");}} Sql.append (")"). Append ("VALUES ("). Append (SQL1). Append (");"); Try{system.out.println (Sql.tostring ());p s = conn.preparestatement (sql.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) {f = true;}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);}   return F;} Universal Update public static Boolean update (Object ob) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Class C1 = Ob.getclass (); Field[] fi = C1.getdeclaredfields (); StringBuffer sb = new StringBuffer (); Sb.append ("Update"). Append (C1.getsimplename ()). Append ("set"); for (int i = 1; i<fi.length;i++) {sb.append (Fi[i].getname ()); Sb.append ("=?"); if (i!= fi.length-1) {sb.append (",");}} Sb.append ("where"); Sb.append (Fi[0].getname ()). Append ("=?"); Try{system.out.println (Sb.tostring ());p s = conn.preparestatement (sb.tostring ()); for (int i=1;i<fi.length; i++) { Fi[i].setaccessible (True);p s.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 ();} return b;} Universal Delete public static Boolean delete (Class c1, int id) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedStatement PS = null; Field[] fi = C1.getdeclaredfields (); String Sql = "Delete from" +c1.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 (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);} return b;} Universal Delete public static Boolean delete (Class C1, String Wheresql) {Boolean b = false; Connection conn = Baseconnection.getconnection (); PreparedstaTement PS = null; Field[] fi = C1.getdeclaredfields (); String Sql = "Delete from" +c1.getsimplename () + "" +wheresql;try{ps = Conn.preparestatement (SQL); int a = Ps.executeupdate ( ); if (a>0) {b=true;}} catch (Exception e) {e.printstacktrace ();} Finally{baseconnection.closeres (conn, PS);} return b;}}



The universal JDBC Tool class. Simple processing of database operations directly through reflection mechanism

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.