java连接Oracle数据库

第一步 引用 ojdbc.jar 

详见 https://www.cnblogs.com/hailexuexi/p/15062168.html

注:项目代码换路径后要 重新引用

 注:编译项目时也要把 ojdbc6.jar 打到包里

 完整的java代码

OracleUtil.java
package com.JavaRabbitMQToDataBase.dbOracle;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OracleUtil {
    protected static final Logger logger = LoggerFactory.getLogger(OracleUtil.class);
    //是否连接数据库
    public static String oracle_open="false";
    //数据库连接地址
    public static String oracle_url="jdbc:oracle:thin:@192.168.1.192:1521:orcl";
    //用户名
    public static String oracle_username= "sys as sysdba";
    //密码
    public static String oracle_password ="admindljjk";//
    //驱动名称
    public static String oracle_jdbcName = "oracle.jdbc.OracleDriver";

    /*获取数据库连接 */
    public static Connection getCon(){
        //System.out.println("Oracle getCon00000: "+ oracle_url+" -- " +oracle_username+ " -- "+oracle_password+ " -- " + oracle_jdbcName );
        //if(oracle_open.equals("true")==true){
        //    logger.warn("OracleUtill getCon() 不连接数据库 "+ oracle_url+" -- " +oracle_username+ " -- "+oracle_password+ " -- " + oracle_jdbcName );
        //    return null;
        //}
        try {
            Class.forName(oracle_jdbcName);
        } catch (ClassNotFoundException e) {
            //System.out.println("oracle_jdbcName111111: "+e.getMessage());
            logger.error("OracleUtill getCon() Oracle JDBC驱动未找到!"+ oracle_url+" -- " +oracle_username+ " -- "+oracle_password+ " -- " + oracle_jdbcName +e.getMessage()+e.toString());
            e.printStackTrace();
        }
        Connection con = null;
        try {
            con = DriverManager.getConnection(oracle_url,oracle_username,oracle_password);
        } catch (SQLException e) {
            //System.out.println("oracle_url22222: "+e.getMessage());
            logger.error("OracleUtill getCon() 连接数据库失败! "+ oracle_url+" -- " +oracle_username+ " -- "+oracle_password+ " -- " + oracle_jdbcName +e.getMessage()+e.toString());
            e.printStackTrace();
        }
        return con;
    }

    /*关闭数据库连接*/
    public static void closeCon(Connection con) throws SQLException {
        if (con != null)
            con.close();
    }

    public static String connectionTest()throws SQLException {
        Connection connection= getCon();
        String strResult=connection.toString();
        connection.close();
        return  strResult;
    }

    /*
    public static void main(String[] args){
        try {
            getCon();
            System.out.println("数据库连接成功");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("数据库连接失败");
        }
    }
    */

}

调用方法(示例)

    //连接对象
    Connection connection=null;
    //创建预编译对象
    PreparedStatement ps=null;
    //创建结果集
    ResultSet rs = null;

try { connection = OracleUtil.getCon();//连接对象 //{"event":"satisfaction","callid":"1635822668.528","surveyresult":"2","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 surveyresult = jsonObj.get("surveyresult").toString(); String sn = jsonObj.get("sn").toString(); sql = "insert into CTI_SATISFACTION "; sql = sql + " ( ID,event,callid,surveyresult,sn ) "; sql = sql + " values(?,?,?,?,? )"; ps = connection.prepareStatement(sql); ps.setString(1, strId); ps.setString(2, event); ps.setString(3, callid); ps.setString(4, surveyresult); ps.setString(5, sn); //logger.debug("dbCDR insert() 执行SQL!" + ps.toString() ); result = ps.executeUpdate(); } catch (Exception e) { e.printStackTrace(); logger.error("dbSatisfaction insert() 执行SQL出错! " + result + " " + callid + " " + sql + " " + e.toString()); } finally { try {
          if(ps!=null){ ps.close(); }//ps需要关闭,不然会出现 超出打开游标的最大数 的错误  OracleUtil.closeCon(connection); }
catch (SQLException e) { e.printStackTrace(); logger.error("dbSatisfaction insert() 关闭连接出错!" + result + " " + callid + " " + " " + sql + " " + e.toString()); } }

 

posted @ 2024-07-15 10:55  海乐学习  阅读(17)  评论(0编辑  收藏  举报