CRUD of the package just need a method may be transferred to the object as a parameter

insert

 / **
* class name correspondence table, the corresponding attribute field
* @param obj object passed
* @return
* /
public static Boolean INSERT (Object obj) {
Boolean = In Flag to false;
<?> = Obj.getClass Class C () ; // get the Class obj

// StringBuffer insert using SQL statements configured
// Get the name of the class name mapping table by reflecting
StringBuffer SB1 = new new StringBuffer ( "INSERT INTO" c.getSimpleName + () + "(");
StringBuffer SB2 = new new StringBuffer ( "values ( ");

Field [] field = c.getDeclaredFields () ; // get the object by reflection property array
for (int i = 0; i <field.length; i ++) {// attributes configured to traverse the SQL statement

IF (! = I-field.length. 1) {
. sb1.append (Field [I] .getName ()) the append ( ",");
sb2.append ( "?,");
} the else {
sb1.append ( . Field [I] .getName ()) the append ( ")");
sb2.append ( ");?");
}
}
String SQL = sb1.append (SB2) .toString ();
// get a database connection, database operations
Connection Conn = the getConnection ();
the PreparedStatement PS = null;
the try {
PS = conn.prepareStatement (SQL);
for (int I = 0; I <field.length; I ++) {
Field [I] .setAccessible (to true ); // set the accessibility attributes, access to the private property
try {// (object) method to get the object property value of the object by get Field
ps.setObject (i + 1, field [ i] .get (obj) ); // SQL statement is precompiled in? Assignment
} the catch (an IllegalArgumentException E) {
e.printStackTrace ();
The catch} (IllegalAccessException E) {
e.printStackTrace ();
}
}
System.out.println (PS); // return execution of a SQL statement
flag = ps.execute (); // execute SQL
} the catch (SQLException E) {
e.printStackTrace ();
} {the finally
Close (PS);
Close (Conn);
}
return In Flag;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
46 is
47
48
49
50
51 is
 2 deleted
 

 / **
* Remove the main key (default attribute first) object
* @param obj
* @return
* /
public static Boolean Delete (Object obj) {
Boolean = In Flag to false;
Class C = obj.getClass () <?>; // get the obj Class
// delete constructed SQL statement
StringBuffer SB = new new StringBuffer ( "from the delete");
. sb.append (c.getSimpleName ()) the append ( "the WHERE");
// get an array of object properties
Field [] = c.getDeclaredFields Field ();
// set first property accessibility
Field [0] .setAccessible (to true);
// get the first name of the attribute configured to delete SQL
sb.append (Field [ . 0] .getName ()) the append ( "=?");
String SQL = sb.toString ();
Connection Conn = the getConnection ();
the PreparedStatement PS = null;
the try {
PS = conn.prepareStatement (SQL);
ps.setObject(1, field[0].get(obj));
System.out.println(ps);
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}finally {
close(ps);
close(conn);
}
return flag;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
 3, modify
 

/ **
* analog jdbc updating operation, the first default primary key attribute
* @param obj
* @return
* /
public static Boolean Update (Object obj) {
Boolean = In Flag to false;
Class C = obj.getClass (<?> );
the StringBuffer the StringBuffer new new SB = ( "Update" c.getSimpleName + () + "SET");

Field[] field = c.getDeclaredFields();
for(int i = 1; i < field.length; i++) {
if(i != field.length-1) {
sb.append(field[i].getName()).append("=?,");
}else {
sb.append(field[i].getName()).append("=? where ");
}
}
sb.append(field[0].getName() + "=?");
String sql = sb.toString();

Connection conn = getConnection();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for(int i = 1; i < field.length; i++) {
field[i].setAccessible(true);
ps.setObject(i, field[i].get(obj));
}
field[0].setAccessible(true);
ps.setObject(field.length, field[0].get(obj));
System.out.println(ps);
flag = ps.execute();
} catch (SQLException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}finally {
close(ps);
close(conn);
}
return flag;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
36
37 [
38 is
39
40
41 is
42 is
43 is
44 is
45
 4 query, query operations do not need to pass an object, just a class class can
 

/ **
* simulation framework, all the records acquired by correspondence table Class objects
* @param obj
* @return
* /
public static <T> List <T> a selectAll (Class <T> C) {
String = SQL "SELECT * from "+ c.getSimpleName (); // get the class name correspondence table SQL statement is constructed by a reflection
List <T> list = new ArrayList <T> (); // store query results
Field [] field = c.getDeclaredFields (); // get all the properties by reflection
Connection Conn = the getConnection ();
the PreparedStatement PS = null;
the ResultSet RS = null;
the try {
PS = conn.prepareStatement (SQL);
System.out.println (PS);
RS = PS .executeQuery (); // return a result set
the while (rs.next ()) {
T obj = c.newInstance (); // configured by a reflection type instance T
for (int i = 0; i <field.length ; i ++) {//

field [i] .setAccessible (true) ; // set the accessibility attributes (access to the private property)
Field [I] .set (obj, rs.getObject (Field [I] .getName ())); // Get the name of the result set by the attribute values to be the object instance
}
List.add (obj); // add to the list the instance object set
}
} the catch (SQLException E) {
e.printStackTrace ();
} the catch (an InstantiationException is E) {
e.printStackTrace ();
} the catch (IllegalAccessException E) {
e.printStackTrace (http://www.my516.com);
} {the finally
Close (RS);
Close (PS);
Close (Conn);
}
return List ;
}
---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/11335131.html