日期:2014-05-16 浏览次数:20453 次
菜鸟写的JDBC封装,望前辈指出不足地方!
先介绍一下功能: 1.数据库连接地址从配置文件中获取
??????????????????????????2.支持命名参数设置(如: select? * from t_user where f_userName = :username)
??????????????????????????3.批量插入、更新?使用同一个connncetion
使用:
?
public class StockSend { Log LOG = LogFactory.getLog(StockSend.class); WarningEnvironment env = new WarningEnvironment(); SaveRemind send = new SaveRemind(); MySqlUtil mySqlUtil = new MySqlUtil(env.db_stock_warning, env.DB_STOCK_USER, env.DB_STOCK_PASSWORD); public List<StockSendUser> sendAllUser(){ List<StockSendUser> stockSends = new ArrayList<StockSendUser>(); String sql = "select * from db_stock_warning.t_warning_task where f_send_state = 0"; stockSends = mySqlUtil.queryForList(sql, callBack); return stockSends; } MysqlQueryCallBack callBack = new MysqlQueryCallBack() {//查询对象封装回调接口 @Override public Object next(ResultSet rs) throws Exception { StockSendUser stock = new StockSendUser(); stock.setUin(rs.getString("f_uin")); stock.setStock_code(rs.getString("f_stock_code")); stock.setStock_name(rs.getString("f_stock_name")); stock.setPrice(rs.getDouble("f_price")); stock.setStock_higher(rs.getDouble("f_stock_higher")); stock.setStock_less(rs.getDouble("f_stock_less")); return stock; } }; public void updateStockSend(StockSendUser stock){ String sql="update from db_stock_warning.t_warning_task set f_send_state = 1 where f_uin= ? and f_date= ? and f_stock_code=? and f_price = ? "; mySqlUtil.insertOrUpdate(sql, parameter, stock); } MysqlParameterCallBack parameter = new MysqlParameterCallBack() {//参数设置回调接口 @Override public void setParameter(PreparedStatement ps, Object o) throws Exception { StockSendUser stock = (StockSendUser)o; int i=1; ps.setString(i++, stock.getUin()); ps.setString(i++, stock.getDate()); ps.setString(i++, stock.getStock_code()); ps.setDouble(i++, stock.getPrice()); } }; 核心类: package com.tenpay.fund.util.pub.JDBC; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * jdbc连接 处理类 * @author W_xieqi * */ public class MySqlUtil { private final String mysql_url; private final String user ; private final String password ; private Log log = LogFactory.getLog(MySqlUtil.class); public MySqlUtil(String mysql_url,String user,String password ) { this.mysql_url= mysql_url; this.user = user; this.password = password; } //获得连接 public Connection getConnection() { // 建立连接 Connection con = null; try { if (con == null || con.isClosed()) { Class.forName("com.mysql.jdbc.Driver");//这里应该也应该放到配置文件中 con = DriverManager.getConnection(mysql_url, user, password); } } catch (Exception e) { log.error("re create connection error", e); } return con; } /** * 关闭连接方法 */ public void closeConnection(Connection con){ try { //连接不为空,并且连接未关闭 if(con !=null && !con.isClosed()){ con.close(); } } catch (SQLException e) { log.error("close connection error", e); } } /** * 关闭PreparedStatement */ public void closePreparedStatement(PreparedStatement ps){ try { if(ps != null && !ps.isClosed()){ ps.close(); } } catch (SQLException e) { log.error("close PreparedStatement excption",e); } } /** * 关闭PreparedStatement */ public void closeNamedParameterStatement(NamedParameterStatement ps){ try { if(ps != null ){ ps.close(); } } catch (SQLException e) { log.error("closeNamedParameterStatement excption",e); } } /** * 关闭PreparedStatement */ public void closeResultSet(ResultSet rs){ try { if(rs != null && !rs.isClosed()){ rs.close(); }