连接-》操作-》断开连接
这种方式的特点是 每次都是新的连接,单线程,缺点是 网络环境不好时连接oracle比较费时。
OracleUtil.java 基础类代码 详见:https://www.cnblogs.com/hailexuexi/p/18302732
完整代码
dbCDR.java
package com.JavaRabbitMQToDataBase.dbOracle;import java.util.UUID; import com.alibaba.fastjson.JSONObject;import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class dbCDR {protected static final Logger logger = LoggerFactory.getLogger(dbCDR.class);//连接对象Connection connection=null;//创建预编译对象PreparedStatement ps=null;//创建结果集ResultSet rs = null;//CDR 参数private String _eventJson;public dbCDR(String eventJson){_eventJson=eventJson;}//插入public int insert() {logger.debug("dbCDR insert() CDR 准备执行SQL" + this._eventJson);int result = 0;String sql = "";String callid ="";String callfrom ="";if (OracleUtil.oracle_open.equals("true") == false) {logger.warn("dbCDR insert() CDR 没有启动连接数据库 " + OracleUtil.oracle_open + " " + _eventJson);return -2;}try {connection = OracleUtil.getCon();//连接对象//{"event":"NewCdr","callid":"1627349862.433","timestart":"2021-07-27 09:37:42","callfrom":"1008","callto":"1009"// ,"callduraction":"16","talkduraction":"11","srctrunkname":"","dsttrcunkname":"","pincode":"","status":"ANSWERED"// ,"type":"Internal","callPath":"","recording":"20210727093747-1627349862.433-1008-1009-Internal.wav","didnumber":"","agentringtime":"0"// ,"sn":"369264842620"}String strId = UUID.randomUUID().toString();//唯一码JSONObject jsonObj = JSONObject.parseObject(this._eventJson);// String event = jsonObj.get("event").toString();callid = jsonObj.get("callid").toString();String timestart = jsonObj.get("timestart").toString();//主叫号码callfrom = jsonObj.get("callfrom").toString();//被叫号码String callto = jsonObj.get("callto").toString();//如果有 6703(1008) 时将队列号6703 去掉if(callto.contains("(")==true){callto = callto.substring(5); //6703(callto = callto.substring(0, callto.indexOf(")"));//去掉 ) }String callduraction = jsonObj.get("callduraction").toString();String talkduraction = jsonObj.get("talkduraction").toString();String srctrunkname = jsonObj.get("srctrunkname").toString();String dsttrcunkname = jsonObj.get("dsttrcunkname").toString();String pincode = jsonObj.get("pincode").toString();String status = jsonObj.get("status").toString();String type = jsonObj.get("type").toString();String recording = jsonObj.get("recording").toString();String didnumber = jsonObj.get("didnumber").toString();String agentringtime = jsonObj.get("agentringtime").toString();String sn = jsonObj.get("sn").toString();String callPath = jsonObj.get("callPath").toString();String sessionid = "";// if(jsonObj.containsKey("sessionid")==true){sessionid=jsonObj.get("sessionid").toString();}String agentname = "";if(jsonObj.containsKey("agentname")==true){agentname=jsonObj.get("agentname").toString();}sql = "insert into CTI_CDR ";sql = sql + " ( ID,event,callid,timestart,callfrom, ";sql = sql + " callto,callduraction,talkduraction,srctrunkname,dsttrcunkname, ";sql = sql + " pincode,status,type,recording,didnumber, ";sql = sql + " agentringtime,sn,callpath ,agent_name,sessionid) ";sql = sql + " values(?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,?, ?,?,?,?,? )";logger.error("dbCDR insert() CDR 准备执行SQL " + sql + " " );ps = connection.prepareStatement(sql);ps.setString(1, strId);ps.setString(2, event);ps.setString(3, callid);ps.setString(4, timestart);ps.setString(5, callfrom);ps.setString(6, callto);ps.setString(7, callduraction);ps.setString(8, talkduraction);ps.setString(9, srctrunkname);ps.setString(10, dsttrcunkname);ps.setString(11, pincode);ps.setString(12, status);ps.setString(13, type);ps.setString(14, recording);ps.setString(15, didnumber);ps.setString(16, agentringtime);ps.setString(17, sn);ps.setString(18, callPath);ps.setString(19, agentname);ps.setString(20, sessionid);//logger.debug("dbCDR insert() CDR 执行SQL!" + ps.toString() ); result = ps.executeUpdate();} catch (Exception e) {e.printStackTrace();logger.error("dbCDR insert() CDR 执行SQL出错! " + result + " "+ callid+ " "+callfrom+ " " + sql + " " + e.toString());} finally {try {if (ps != null) { // ps.close();////ps需要关闭,不然会出现 java.sql.SQLException: ORA-01000: 超出打开游标的最大数 }OracleUtil.closeCon(connection);} catch (SQLException e) {e.printStackTrace();logger.error("dbCDR insert() CDR 关闭连接出错!" + result + " "+ callid+ " "+callfrom+ " " + " " + sql + " " + e.toString());}}logger.debug("dbCDR insert() CDR 执行SQL成功!" + result + " "+ callid+ " "+callfrom+ " " + sql);return result;}//查询public void select(){connection = OracleUtil.getCon();String sql = "select ID,AGENT_NAME,DATE_TIME,STATUS from CTI_AGENT_WORK_REST";try {ps = connection.prepareStatement(sql);rs = ps.executeQuery();while (rs.next()){int id = rs.getInt(1);String AGENT_NAME = rs.getString(2);String DATE_TIME = rs.getString(3);String STATUS= rs.getString(4);System.out.println("ID:"+id + " AGENT_NAME:"+AGENT_NAME+" DATE_TIME:"+DATE_TIME+" STATUS:"+STATUS);}} catch (SQLException e) {e.printStackTrace();}finally {try {OracleUtil.closeCon(connection);}catch (SQLException e) {e.printStackTrace();}}}//修改public int update(){connection = OracleUtil.getCon();String sql = "update student set name = ?,gender = ? where id = ?";int result = 0;try {ps = connection.prepareStatement(sql);ps.setString(1,"老张");ps.setString(2,"男");ps.setInt(3,2);result = ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();}finally {try {OracleUtil.closeCon(connection);}catch (SQLException e) {e.printStackTrace();}}return result;}//删除public int delete(){int result = 0;connection = OracleUtil.getCon();String sql = "delete from student where id = ?";try {ps = connection.prepareStatement(sql);ps.setInt(1,1);result = ps.executeUpdate();} catch (SQLException e) {e.printStackTrace();}finally {try {OracleUtil.closeCon(connection);}catch (SQLException e) {e.printStackTrace();}}return result;} }
调用方法
dbCDR objCDR = new dbCDR(eventJson); objCDR.insert();