日期:2014-05-16 浏览次数:20576 次
package org.gfg.dbutil.common; import java.io.IOException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; @SuppressWarnings("static-access") public class Commons { private static DataSource dataSource; static { try { Properties prop = new Properties(); prop.load(Commons.class.getClassLoader().getResourceAsStream( "dbcpconfig.properties")); BasicDataSourceFactory factory = new BasicDataSourceFactory(); dataSource = factory.createDataSource(prop); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static Connection getConnection() throws SQLException { return dataSource.getConnection(); } public static void close(ResultSet rs, PreparedStatement ps, Connection conn) { try { if (rs != null) { rs.close(); } if (ps != null) { ps.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
package org.gfg.dbutil.common; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import org.gfg.dbutil.handler.BeanHandler; import org.gfg.dbutil.handler.IResultSetHandler; public class DbUtils { public static void insert(String sql, Object[] params) throws SQLException {// insert Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = Commons.getConnection(); ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } ps.executeUpdate(); } finally { Commons.close(rs, ps, conn); } } public static void update(String sql, Object[] params) throws SQLException {// update DbUtils.insert(sql, params); } public static void delete(String sql, Object[] params) throws SQLException {// delete DbUtils.insert(sql, params); } public static Object query(String sql, Object[] params, IResultSetHandler rsh) throws SQLException { Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = Commons.getConnection(); ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } rs = ps.executeQuery(); return rsh.handler(rs); } finally { Commons.close(rs, ps, conn); } } public static Object load(String sql, Object[] params,Class<?> clazz) { IResultSetHandler rsh=new BeanHandler(clazz); Connection conn = null; PreparedStatement ps = null; ResultSet rs = null; try { conn = Commons.getConnection(); ps = conn.prepareStatement(sql); for (int i = 0; i < params.length; i++) { ps.setObject(i + 1, params[i]); } rs = ps.executeQuery(); return rsh.handler(rs); } catch (SQLException e) { e.printStackTrace(); } finally { Commons.close(rs, ps, conn); } return null; } }
package org.gfg.dbutil.handler; import java.sql.ResultSe