使用jdbc对接oracle数据库并回读数据

本文详细介绍了如何使用Java JDBC API连接Oracle数据库,并实现数据的回读操作。首先,需要配置Oracle数据库的JDBC驱动,然后创建数据库连接,编写SQL语句,执行查询操作,最后通过ResultSet获取并处理查询结果。通过实例代码,读者可以掌握Java与Oracle数据库交互的基本步骤。
摘要由CSDN通过智能技术生成
public class DBUtils {

    @Value("${spring.account.driver}")
    private String driver;
    @Value("${spring.account.url}")
    private String url;
    @Value("${spring.account.userName}")
    private String userName;
    @Value("${spring.account.password}")
    private String password;
    @Value("${jasypt.encryptorPwd.password}")
    private String encPassword;

    private Connection conn = null;

    /**
     * 解密
     * @param pwd
     * @return
     */
    public String decryptPassword(String pwd){
        String result = null;
        BasicTextEncryptor textEnc = new BasicTextEncryptor();
        textEnc.setPassword(encPassword);
        if(PropertyValueEncryptionUtils.isEncryptedValue(pwd)){
            result = PropertyValueEncryptionUtils.decrypt(pwd, textEnc);
        }
        return result;
    }

    public synchronized Connection getConnection() throws Exception {
        Class.forName(driver); //加载驱动
        if(null == conn){
            //创建连接对象
            conn = DriverManager.getConnection(url, userName, decryptPassword(password.substring(3)));
        }
        return conn;
    }

    /**
     * 将ResultSet对象转换成domain的List
     * @param rs
     * @return
     * @throws Exception
     */
    public List<JournalInterface> bindDataToDTO(ResultSet rs) throws Exception {

        JournalInterface dto = new JournalInterface();
        Method[] methods = dto.getClass().getMethods();
        List<JournalInterface> dtoList = new ArrayList();
        ResultSetMetaData meta = rs.getMetaData();
        JournalInterface obj = null;
        while (rs.next()) {
            // 获取实例对象
            obj = dto.getClass().newInstance();
            // 循环获取指定行的每一列的信息
            for (int i = 1; i <= meta.getColumnCount(); i++) {
                String colName = meta.getColumnName(i);
                String setMethodName = "set" + colName;

                //遍历Method
                for (int j = 0; j < methods.length; j++) {
                    if (methods[j].getName().equalsIgnoreCase(setMethodName)) {
                        setMethodName = methods[j].getName();
                        // 获取当前位置的值,返回Object类型
                        Object value = rs.getObject(colName);
                        if(value == null){
                            continue;
                        }
                        //实行Set方法
                        try {
                            if(value instanceof BigDecimal){
                                //value = ((BigDecimal) value).toString();
                                value = Long.parseLong(value.toString());
                            }
                            //利用反射获取对象
                            //JavaBean内部属性和ResultSet中一致时候
                            Method setMethod = obj.getClass().getMethod(
                                    setMethodName, value.getClass());
                            setMethod.invoke(obj, value);
                        } catch (Exception e) {
                            //JavaBean内部属性和ResultSet中不一致时候,使用String来输入值。
                            Method setMethod = dto.getClass().getMethod(
                                    setMethodName, String.class);
                            e.printStackTrace();
                        }
                    }
                }
            }
            dtoList.add(obj);
        }
        return dtoList;
    }

    /**
     * 释放连接
     * @param rs
     * @param statement
     * @param con
     * @throws SQLException
     */
    public void release(ResultSet rs, Statement statement, Connection con) throws SQLException{

        try {
            if(rs != null){
                rs.close();
            }
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        try {
            if(statement != null){
                statement.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            if(con != null){
                con.close();
            }
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值