java中数据库连接jdbc的编写

来源:互联网 发布:2015网络流行歌曲 编辑:程序博客网 时间:2024/06/05 11:27
public void repayDataTrans(){LOG.info("RepayDataTrans start.");Connection conn = null;PreparedStatement ps = null;ResultSet rs = null;try{Class.forName("com.mysql.jdbc.Driver");String url = "jdbc:mysql://120.26.41.212:3306/db_hbxj?useUnicode=true&characterEncoding=utf-8";//类加载和URL对于不同的数据库有不同的编写方式,类加载也不一样;String userName = "***";String userPass = "******";conn = DriverManager.getConnection(url, userName, userPass);Date curDate = DateUtils.truncate(new Date(), Calendar.DATE);Date preDate = DateUtils.addHours(curDate, -5);Date nextDate = DateUtils.addHours(curDate, 22);long preLong = preDate.getTime()/1000;long nextLong = nextDate.getTime()/1000;String sql = "select id,fullname,loan_id,optime,status,over_days,over_money,hk_sjje,charge_id,hk_ly from xj_repay_17 where status = 1"+ " and optime >= ? and optime < ? ";ps = conn.prepareStatement(sql);ps.setLong(1, preLong);ps.setLong(2, nextLong);rs = ps.executeQuery();while(rs.next()){long id = rs.getLong("id")+48447;LOG.info("id = {}",id);int overDays = rs.getInt("over_days");LOG.info("overDays = {}",overDays);int overFee = rs.getInt("over_money");LOG.info("overFee = {}",overFee);double repayAmount = rs.getDouble("hk_sjje");LOG.info("repayAmount = {}",repayAmount);Date repayDate = new Date(rs.getInt("optime")*1000L);LOG.info("repayDate = {}",repayDate);String chargeId = rs.getString("charge_id");LOG.info("chargeId = {}",chargeId);String repaySource = rs.getString("hk_ly");LOG.info("repaySource = {}",repaySource);loanRepayService.repay(id, overDays, overFee, repayAmount, repayDate, chargeId, repaySource);}}catch(Exception e){LOG.error("RepayDataTrans error.", e);}finally{try {if(rs!=null){rs.close();rs = null;}if(ps!=null){ps.close();ps = null;}if(conn!=null){conn.close();conn = null;}} catch (SQLException e) {e.printStackTrace();}LOG.info("RepayDataTrans complement.");}}

0 0
原创粉丝点击