[오리지널] 유니버설 대상 조회의dao

21752 단어 대상을 향하다

import java.lang.reflect.Method;
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.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.lowca.robot.Crawler;

/**
 * <p>
 *   dao ,       :
 * </p>
 * <p>
 * 1.     
 * </p>
 * <p>
 * 2.      
 * </p>
 * <p>
 * 3.          java pojo         /p>
 * <p>
 * 4.      “id”,    Integer  ,                 
 * </p>
 * <p>
 * 5.       
 * </p>
 * 
 * @author kong
 * 
 */
public class Dao {

	private static final String DRIVER = "org.sqlite.JDBC";
	private static final String CONNECT_URL = "jdbc:sqlite:c:/robot.db";
	private static final String USERNAME = null;
	private static final String PASSWORD = null;
	private static final Log log = LogFactory.getLog(Crawler.class);

	static {
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			throw new RuntimeException("  JDBC    :
" + e.getMessage()); } } /** * * * @return * @throws SQLException */ private Connection getConnection() throws SQLException { Connection conn = null; if (USERNAME != null && PASSWORD != null) conn = DriverManager.getConnection(CONNECT_URL, USERNAME, USERNAME); else conn = DriverManager.getConnection(CONNECT_URL); conn.setAutoCommit(false); return conn; } /** * * * @param <T> * @param clazz * @param rs * @return * @throws SQLException */ private <T> T mapping(Class<T> clazz, ResultSet rs) throws SQLException { T t = null; if (rs.next()) { try { t = clazz.newInstance(); } catch (Exception e) { throw new RuntimeException(e); } ResultSetMetaData metadata = rs.getMetaData(); int size = metadata.getColumnCount(); for (int i = 0; i < size; i++) { String columnName = metadata.getColumnLabel(i + 1); Object columnValue = rs.getObject(metadata .getColumnLabel(i + 1)); if (columnValue == null) continue; // String propertyName = toJavaCase(columnName); String methodName = "set" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1); Method[] methods = clazz.getMethods(); for (Method method : methods) { if (methodName.equals(method.getName())) { try { String propertyTypeName = method .getParameterTypes()[0].getName(); String columnTypeName = columnValue.getClass() .getName(); if (propertyTypeName .equalsIgnoreCase(columnTypeName)) method.invoke(t, columnValue); else { // if ("java.util.Date".equals(propertyTypeName) && "java.lang.Long" .equals(columnTypeName)) method.invoke(t, new Date( (Long) columnValue)); // if ("java.lang.Boolean" .equals(propertyTypeName) && "java.lang.Integer" .equals(columnTypeName)) method.invoke(t, columnValue.toString() .equals("0") ? false : true); } } catch (Exception e) { throw new RuntimeException(e); } } } } } return t; } /** * * * @param rs * @param ps * @param conn */ public void free(ResultSet rs, PreparedStatement ps, Connection conn) { try { if (rs != null) rs.close(); } catch (Exception e) { log.error(this, e); } finally { try { if (ps != null) ps.close(); } catch (Exception e) { log.error(this, e); } finally { if (conn != null) { try { conn.close(); } catch (SQLException e) { log.error(this, e); } } } } } /** * * * @param <T> * @param clazz * @param sql * @param values * @return * @throws SQLException */ public <T> T query(Class<T> clazz, String sql, Object[] values) throws SQLException { Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); if (values != null) { for (int i = 0; i < values.length; i++) { ps.setObject(i + 1, values[i]); } } ResultSet rs = ps.executeQuery(); T t = mapping(clazz, rs); free(rs, ps, conn); return t; } /** * ( ) * * @param <T> * @param clazz * @param sql * @return * @throws SQLException */ public <T> T query(Class<T> clazz, String sql) throws SQLException { return query(clazz, sql, null); } /** * * * @param <T> * @param clazz * @param sql * @param values * @return * @throws SQLException */ public <T> List<T> queryList(Class<T> clazz, String sql, Object[] values) throws SQLException { Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); if (values != null) { for (int i = 0; i < values.length; i++) { ps.setObject(i + 1, values[i]); } } ResultSet rs = ps.executeQuery(); List<T> list = new ArrayList<T>(); T t = null; while ((t = mapping(clazz, rs)) != null) { list.add(t); } free(rs, ps, conn); return list; } /** * ( ) * * @param <T> * @param clazz * @param sql * @return * @throws SQLException */ public <T> List<T> queryList(Class<T> clazz, String sql) throws SQLException { return queryList(clazz, sql, null); } /** * * * @param clazz * @return * @throws SQLException */ public int count(Class<?> clazz) throws SQLException { Connection conn = getConnection(); String tableName = toDbCase(clazz.getSimpleName()); String sql = "select count(*) from " + tableName; PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); int num = 0; if (rs.next()) num = rs.getInt(1); free(rs, ps, conn); return num; } /** * sql * * @param sql * @param values * @return * @throws SQLException */ public int count(String sql, Object[] values) throws SQLException { Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); if (values != null) { for (int i = 0; i < values.length; i++) { ps.setObject(i + 1, values[i]); } } ResultSet rs = ps.executeQuery(); int num = 0; if (rs.next()) num = rs.getInt(1); free(rs, ps, conn); return num; } /** * sql ( ) * * @param sql * @return * @throws SQLException */ public int count(String sql) throws SQLException { return count(sql, null); } /** * id * * @param <T> * @param clazz * @param id * @return * @throws SQLException */ public <T> T get(Class<T> clazz, Integer id) throws SQLException { String tableName = toDbCase(clazz.getSimpleName()); String sql = "select * from " + tableName + " where id=" + id.intValue(); Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); T t = mapping(clazz, rs); free(rs, ps, conn); return t; } /** * * * @param clazz * @param id * @return * @throws SQLException */ public int delete(Class<?> clazz, Integer id) throws SQLException { String tableName = toDbCase(clazz.getSimpleName()); String sql = "delete from " + tableName + " where id=" + id.intValue(); Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); int rowCount = ps.executeUpdate(); free(null, ps, conn); return rowCount; } /** * * * @param object * @throws SQLException */ public void save(Object object) throws SQLException { // Method[] methods = object.getClass().getMethods(); Map<String, Object> kvMap = new HashMap<String, Object>(); for (Method method : methods) { if (method.getName().startsWith("set")) { String key = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); Method getMethod = null; Object value = null; try { getMethod = object.getClass().getDeclaredMethod( method.getName().replaceFirst("set", "get")); value = getMethod.invoke(object); } catch (Exception e) { throw new RuntimeException(e); } kvMap.put(key, value); } } // sql String tableName = toDbCase(object.getClass().getSimpleName()); Object[] values = new Object[kvMap.size()]; StringBuffer sb = new StringBuffer("INSERT INTO " + tableName + "("); StringBuffer params = new StringBuffer(); int index = 0; for (String key : kvMap.keySet()) { String columnName = toDbCase(key); sb.append(columnName + ","); params.append("?,"); values[index] = kvMap.get(key); index++; } if (sb.charAt(sb.length() - 1) == ',') sb.delete(sb.length() - 1, sb.length()); if (params.charAt(params.length() - 1) == ',') params.delete(params.length() - 1, params.length()); sb.append(") VALUES(").append(params).append(");"); String sql = sb.toString(); // sql Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); if (values != null) { for (int i = 0; i < values.length; i++) { ps.setObject(i + 1, values[i]); } } conn.setAutoCommit(true); ps.execute(); conn.setAutoCommit(false); // ps = conn.prepareStatement("select last_insert_rowid()"); ResultSet rs = ps.executeQuery(); Integer pk = 0; if (rs.next()) pk = rs.getInt(1); // try { Method method = object.getClass().getDeclaredMethod("setId", Integer.class); method.invoke(object, pk); } catch (Exception e) { throw new RuntimeException(e); } free(null, ps, conn); } /** * * * @param object * @throws SQLException */ public void update(Object object) throws SQLException { // Method[] methods = object.getClass().getMethods(); Map<String, Object> kvMap = new HashMap<String, Object>(); for (Method method : methods) { if (method.getName().startsWith("set")) { String key = method.getName().substring(3, 4).toLowerCase() + method.getName().substring(4); Method getMethod = null; Object value = null; try { getMethod = object.getClass().getDeclaredMethod( method.getName().replaceFirst("set", "get")); value = getMethod.invoke(object); } catch (Exception e) { throw new RuntimeException(e); } kvMap.put(key, value); } } // sql String tableName = toDbCase(object.getClass().getSimpleName()); Object[] values = new Object[kvMap.size()]; StringBuffer sb = new StringBuffer("UPDATE " + tableName + " "); int index = 0; Integer id = 0; boolean firstTime = true; for (String key : kvMap.keySet()) { String columnName = toDbCase(key); if (key.equalsIgnoreCase("id")) { id = (Integer) kvMap.get(key); continue; } sb.append((firstTime ? " SET " : "") + columnName + "=?,"); firstTime = false; values[index] = kvMap.get(key); index++; } values[index] = id; if (sb.charAt(sb.length() - 1) == ',') sb.delete(sb.length() - 1, sb.length()); sb.append(" WHERE id=?;"); String sql = sb.toString(); // sql Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); if (values != null) { for (int i = 0; i < values.length; i++) { ps.setObject(i + 1, values[i]); } } conn.setAutoCommit(true); ps.executeUpdate(); conn.setAutoCommit(false); free(null, ps, conn); } /** * sql * * @param sql * @param values * @return * @throws SQLException */ public boolean execute(String sql, Object[] values) throws SQLException { Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); if (values != null) { for (int i = 0; i < values.length; i++) { ps.setObject(i + 1, values[i]); } } conn.setAutoCommit(true); boolean r = ps.execute(); conn.setAutoCommit(false); free(null, ps, conn); return r; } /** * sql , * * @param sql * @return * @throws SQLException */ public boolean execute(String sql) throws SQLException { return execute(sql, null); } /** * * * @param clazz * @return * @throws SQLException */ public boolean existTable(Class<?> clazz) throws SQLException { String tableName = toDbCase(clazz.getSimpleName()); String sql = "SELECT COUNT(*) AS table_count FROM sqlite_master WHERE TYPE='table' AND NAME=?;"; Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); ps.setObject(1, tableName); ResultSet rs = ps.executeQuery(); int num = 0; if (rs.next()) { num = rs.getInt("table_count"); } free(null, ps, conn); return num > 0 ? true : false; } /** * * * @param clazz * @param delIfExist * ,true ,false * @throws SQLException */ public void createTable(Class<?> clazz, boolean delIfExist) throws SQLException { // , boolean existTable = existTable(clazz); if (!delIfExist && existTable) { return; } if (delIfExist && existTable) { deleteTable(clazz); } // Method[] methods = clazz.getMethods(); Map<String, String> kvMap = new HashMap<String, String>(); for (Method method : methods) { if (method.getName().startsWith("set")) { String property = method.getName().substring(3, 4) .toLowerCase() + method.getName().substring(4); String propertyClassName = method.getParameterTypes()[0] .getName(); kvMap.put(property, propertyClassName); } } // sql String tableName = toDbCase(clazz.getName()); StringBuffer sb = new StringBuffer("CREATE TABLE " + tableName + " (id INTEGER PRIMARY KEY,"); for (String key : kvMap.keySet()) { if (key.equalsIgnoreCase("id")) { continue; } String columnName = toDbCase(key); String propertyClassName = kvMap.get(key); String dbTypeName = getDbTypeName(propertyClassName); sb.append(columnName + " " + dbTypeName + ","); } if (sb.charAt(sb.length() - 1) == ',') sb.delete(sb.length() - 1, sb.length()); sb.append(");"); String sql = sb.toString(); // sql Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); conn.setAutoCommit(true); ps.execute(); conn.setAutoCommit(false); free(null, ps, conn); } /** * sql * * @param sql * @throws SQLException */ public void createTable(String sql) throws SQLException { // Connection conn = getConnection(); PreparedStatement ps = conn.prepareStatement(sql); conn.setAutoCommit(true); ps.execute(); conn.setAutoCommit(false); free(null, ps, conn); } /** * , , * * @param clazz * @throws SQLException */ public void createTable(Class<?> clazz) throws SQLException { createTable(clazz, false); } /** * * * @param clazz * @throws SQLException */ public void deleteTable(Class<?> clazz) throws SQLException { String tableName = toDbCase(clazz.getSimpleName()); String sql = "DROP TABLE IF EXISTS " + tableName; execute(sql); } private String getDbTypeName(String propertyClassName) { if ("java.lang.Integer".equals(propertyClassName) || "java.lang.Long".equals(propertyClassName) || "java.lang.Character".equals(propertyClassName) || "java.lang.Short".equals(propertyClassName)) return "INTEGER"; if ("java.lang.Float".equals(propertyClassName) || "java.lang.Double".equals(propertyClassName)) return "REAL"; if ("java.util.Date".equals(propertyClassName)) return "DATETIME"; if ("java.lang.Boolean".equals(propertyClassName)) return "TINYINT"; if ("java.lang.String".equals(propertyClassName)) return "VARCHAR"; return ""; } /** * java * * @param s * @return */ private static String toJavaCase(String s) { if (s == null || s.trim().length() == 0) return s; StringBuffer sb = new StringBuffer(); String[] array = s.split("_"); boolean firstTime = true; for (String e : array) { if (e.length() == 0) continue; else if (e.length() == 1) sb.append(!firstTime ? e.toUpperCase() : e); else sb.append(!firstTime ? (e.substring(0, 1).toUpperCase() + e .substring(1)) : e); firstTime = false; } return sb.toString(); } /** * Java * * @param s * @return */ private static String toDbCase(String s) { if (s == null || s.trim().length() == 0) return s; char[] chars = s.toCharArray(); boolean firstTime = true; StringBuffer sb = new StringBuffer(); for (char c : chars) { if (c >= 'A' && c <= 'Z') { char c1 = (char) (c + 32); sb.append(firstTime ? c1 : "_" + c1); } else sb.append(c); firstTime = false; } return sb.toString(); } public static void main(String[] args) { // System.out // .println(toDbCase("theyBeganToHumWhenSeeingJacksonWalkIntoTheHall")); // System.out // .println(toJavaCase(toDbCase("theyBeganToHumWhenSeeingJacksonWalkIntoTheHall"))); StringBuffer sb = new StringBuffer("sdsdfds1"); sb.delete(sb.length() - 1, sb.length()); System.out.println(sb); } }

좋은 웹페이지 즐겨찾기