Java uses field for common database operations

Source: Internet
Author: User

Last introduced field, it can be run time to get the information in the class, this is a good thing, we can use it to implement database additions and deletions to change the operation

There are, of course, some limitations:

1. Table and entity class fields to correspond

2. Table and entity class names to correspond

3. The first field of the entity class must be the primary key (this is used primarily at the time of the update, depending on the individual situation)

The database operation classes are as follows

public class Reflectdao {private static string uri = "Jdbc:mysql://localhost/reflect";p rivate static string password = "12  3 ";p rivate static string username =" Xhe ";p rivate string insert =" INSERT INTO ";p rivate String update =" Update ";p rivate string delete = "Delete from";p rivate String select = "SELECT * from";p ublic Connection getconnection () {try {class.for Name ("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 (ob j) + "'";} 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 (Ex Ception 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)! = Nu ll) {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].setacces Sible (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 (Ex Ceptione) {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 ();d elete + = 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 (Ex Ception e) {e.printstacktrace ();}}}}
This class is very simple, external calls only need to pass in the corresponding object and class object, you can do the increase and deletion check operation

The test class is as follows

public static void Main (string[] args) {Reflectdao reflect = new Reflectdao (); Student s = new Student (); S.setage (n); S.setname ("Tom"); S.setsex ("man"); S.setstudentid (1130299110); Reflect.save (s), S.getclass ());}
This allows the student information to be saved to the database's Student table (table on 4 fields)


I have to say that field is a good thing, but this is mostly about providing an application, so the above program will have bugs or imperfections, you can use field to write your own method of operation
















Java uses field for common database operations

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.