java取oracle中clob字段数据,并转换成String

private OrderVo getCOLB(OrderVo orderVo ,String id) {

        /**
         * JDBC直连数据库
         */
        Connection conn = null;
        PreparedStatement stmt = null;
        try {
        //String url = "jdbc:oracle:thin:@136.24.252.29:9521:test";
        //String url = "jdbc:oracle:thin:@106.201.174.220:1521:test";
               //String user = "order";
               //String pwd = "****";
       
        String url = PropertiesUtil.getPropertiesForUrl("url");
        String user = PropertiesUtil.getPropertiesForUrl("user");
        String pwd = PropertiesUtil.getPropertiesForUrl("pwd");


            CLOB clob = null;
            String sql = "SELECT * FROM order.info_order where id= " + id;
            DriverManager.registerDriver(new OracleDriver());
            conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象
            stmt = conn.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            String content = "";
            if (rs.next()) {
                clob = (oracle.sql.CLOB) rs.getClob("pic_clob1"); // 获得CLOB字段str
                content = ClobToString(clob);
                orderVo .setPic_x(content);


                clob = (oracle.sql.CLOB) rs.getClob("pic_clob2"); // 获得CLOB字段str
                content = ClobToString(clob);
                orderVo .setPic_y(content);


                clob = (oracle.sql.CLOB) rs.getClob("pic_clob3"); // 获得CLOB字段str
                content = ClobToString(clob);
                orderVo .setPic_z(content);
            }
        } catch (Exception ex) {
            ex.getStackTrace();
        } finally {
            try {
                stmt.close();
                conn.close();
            } catch (Exception e1) {
            e1.getStackTrace();
            }
        }
        return orderVo ;
    }


    public String ClobToString(CLOB clob) throws SQLException, IOException {


        String reString = "";
        Reader is = clob.getCharacterStream();// 得到流
        BufferedReader br = new BufferedReader(is);
        String s = br.readLine();
        StringBuffer sb = new StringBuffer();
        while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
            sb.append(s);
            s = br.readLine();
        }
        reString = sb.toString();
        return reString;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值