You can query any table, encapsulating the data into a query object tools

package com.briup.jdbctest;

import java.lang.reflect.Field;
import java.math.BigDecimal;
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;

public class JDBCUtils {
private static Connection conn = null; // 连接对象
private static String driver = "oracle.jdbc.driver.OracleDriver"; // jar包中的OracleDriver中的类
private static String url = "jdbc:oracle:thin:@//127.0.0.1:1521/orcl";
private static String username = "scott";
private static String password = "tiger";
private static PreparedStatement ps = null;
private static ResultSet rs = null;

public static Connection getConnection() {
try {
Class.forName(driver);
conn = DriverManager.getConnection(url, username, password);

} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;
}

// The data in the table all the objects to be packaged
/ **
*
* generic @param <T> object package
* @param clazz
* @param SQL statement SQL
* @param args parameter passing to a pre-compiled statement
* @return
* /
public static <T> List <T> queryToObj (Class <T> clazz, Conn Connection, SQL String, Object ... args) {
the try {
// Get the result set 1.
PS = conn.prepareStatement (SQL);
for (int. 1 = I; I <= args.length; I ++) {
ps.setObject (I, args [I -. 1]);
}
RS = ps.executeQuery ();

And the results of the query field name // 2. get access to all the fields
ResultSetMetaData metaData = rs.getMetaData ();

// object to save
the ArrayList <T> cols = new new the ArrayList <> ();
the while (rs.next ()) {
// create the object
T obj = clazz.newInstance ();
for (int I =. 1; I <= metaData.getColumnCount (); I ++) {
// Get a corresponding field reflected by
field, Filed = clazz.getDeclaredField (metaData.getColumnLabel (I) .toLowerCase ());
filed.setAccessible (to true);
Object value = RS. the getObject (metaData.getColumnLabel (I));
// number of Oralce BigDecimal is java in the
IF (the instanceof BigDecimal value) {
value = ((BigDecimal) value) .intValue ();
}
filed.set (obj, value) ;
}
cols.add (obj);
}
return cols;
} the catch (Exception E) {
e.printStackTrace ();
} {the finally
Close(conn, ps, rs);
}
return null;
}

// 释放资源
private static void Close(Connection conn, PreparedStatement ps, ResultSet rs) {

if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (ps != null) {
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

public static void main(String[] args) throws SQLException {

//Connection connection = getConnection();
Connection connection = JDBCUTil.getConnection();
String sql = "select * from stu where id=? or id=?";
List<Stu> list = queryToObj(Stu.class, connection, sql, 1, 2);
for (Stu stu : list) {
System.out.println(stu);
}

}
}

Guess you like

Origin www.cnblogs.com/codlover/p/11494000.html