Using Reflection to design Basedao

Source: Internet
Author: User

Package Com.dao.impl;

Import Java.lang.reflect.Field;
Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import java.sql.SQLException;
Import java.util.ArrayList;

Import java.util.List;
/**
* All DAO's parent classes
* date:1111 Year November 11
* @description:
*/
public class Basedao {

protected Connection con;

protected PreparedStatement PS;

protected ResultSet RS;


public void SetConnection () {

try {
Class.forName ("Org.gjt.mm.mysql.Driver");
con = drivermanager.getconnection
("Jdbc:mysql://localhost:3306/product?characterencoding=utf-8", "root", "123456");

catch (ClassNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


}


public void CloseConnection () {

try {

if (Rs!= null) {
Rs.close ();
}
if (PS!= null) {
Ps.close ();
}
if (con!= null) {
Con.close ();
}

catch (Exception e) {
E.printstacktrace ();
}

}

/**
* @Title: UpdateData
* @Description: TODO Update data
* @param: @param SQL SQL statements
* @param: @param valueArray Value List
* @return: void
* @throws
*/
public void UpdateData (String sql,object[] valueArray) {

This.setconnection ();

try {
Execute SQL statement
PS = con.preparestatement (SQL);
Fill placeholder
for (int i=0;i<valuearray.length;i++) {

Ps.setobject (i+1, valuearray[i]);

}
Update Database
Ps.executeupdate ();

catch (SQLException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
This.closeconnection ();
}

}


/**
* @Title: FindData
* @Description: TODO query data
* @param: @param SQL query SQL statement
* @param: @param valueArray Value List
* @param: @param the type of object in the Beanclass collection
* @param: @return Collection of query results
* @return: List
* @throws
*/
Public List FindData (String sql,object[] Valuearray,class beanclass) {

List List = new ArrayList ();

This.setconnection ();

try {
PS = con.preparestatement (SQL);

if (ValueArray!= null) {

for (int i=0;i<valuearray.length;i++) {

Ps.setobject (i+1, valuearray[i]);
}

}

rs = Ps.executequery ();
To get the result set of the examination object
ResultSetMetaData rm = Rs.getmetadata ();
The number of columns that get the query in the result set
int columnCount = Rm.getcolumncount ();

System.out.println (columncount+ "" +rm.getcolumnname (1));

while (Rs.next ()) {

Object beanobj = Beanclass.newinstance ();

There are several columns, loops several times
for (int i=1;i<=columncount;i++) {
Get the column name of the query column
String columnName = Rm.getcolumnname (i);
Get the value of the column name
Object valueobj = Rs.getobject (columnName);

Property object that corresponds to the property name
Field field = Beanclass.getdeclaredfield (ColumnName);
Remove access modifier check
Field.setaccessible (TRUE);
Fills a value with the specified property of an object
Field.set (Beanobj, valueobj);

}
List.add (Beanobj);

}

while (Rs.next ()) {
Generate new entity objects at once per loop
Object beanobj = Beanclass.newinstance ();
//
//
//
If you query only some of the fields, you throw a lot of exceptions that are inefficient
Get all Attributes List
field[] fs = Beanclass.getdeclaredfields ();
//
for (Field Field:fs) {
//
try{
Get Property name
String fieldName = Field.getname ();
Gets the value of the specified column from the result set
Object valueobj = Rs.getobject (fieldName);
//
Remove access modifier check
Field.setaccessible (TRUE);
//
//
Sets the specified property value
Field.set (Beanobj, valueobj);
//
}catch (Exception e) {
//
Continue
//
//     }
//    }



List.add (Beanobj);
//
//   }



catch (Exception e) {

E.printstacktrace ();
}finally{
This.closeconnection ();
}




return list;

}



public static void Main (string[] args) {
//
Basedao dao = new Basedao ();
Dao.setconnection ();
System.out.println (Dao.con);
//
// }


}

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.