java操作oracle大字段CLOB

一、通过mybatis框架,不需要做特殊操作

 

查询

复制代码
 1 <resultMap id="BaseResultMap" type="com.wondersgroup.view360.ui.imagelogo.model.SecurityImageLogo">
 2         <id column="ID" jdbcType="VARCHAR" property="id"/>
 3         <result column="IMAGECODE" jdbcType="VARCHAR" property="imagecode"/>
 4         <result column="IAMGENAME" jdbcType="VARCHAR" property="iamgename"/>
 5         <result column="CREATEUSER" jdbcType="VARCHAR" property="createuser"/>
 6         <result column="CREATETIME" jdbcType="TIMESTAMP" property="createtime"/>
 7         <result column="UPDATEUSER" jdbcType="VARCHAR" property="updateuser"/>
 8         <result column="UPDATETIME" jdbcType="TIMESTAMP" property="updatetime"/>
 9         <result column="IMAGEVALUE" jdbcType="CLOB" property="imagevalue"/>
10     </resultMap>
11 
12 <sql id="Base_Column_List">
13     ID, IMAGECODE, IAMGENAME, CREATEUSER, CREATETIME, UPDATEUSER, UPDATETIME, IMAGEVALUE
14   </sql>
15 
16 <select id="selectByExample" parameterType="com.wondersgroup.view360.ui.imagelogo.model.SecurityImageLogoExample"
17             resultMap="BaseResultMap">
18         select
19         <if test="distinct">
20             distinct
21         </if>
22         <include refid="Base_Column_List"/>
23         from SECURITY_IMAGE_LOGO
24         <if test="_parameter != null">
25             <include refid="Example_Where_Clause"/>
26         </if>
27         <if test="orderByClause != null">
28             order by ${orderByClause}
29         </if>
30     </select>
复制代码

 

插入

1 <insert id="insert" parameterType="com.wondersgroup.view360.ui.imagelogo.model.SecurityImageLogo">
2     insert into SECURITY_IMAGE_LOGO (ID, IMAGECODE, IAMGENAME, 
3       CREATEUSER, CREATETIME, UPDATEUSER, 
4       UPDATETIME, IMAGEVALUE)
5     values (#{id,jdbcType=VARCHAR}, #{imagecode,jdbcType=VARCHAR}, #{iamgename,jdbcType=VARCHAR}, 
6       #{createuser,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{updateuser,jdbcType=VARCHAR}, 
7       #{updatetime,jdbcType=TIMESTAMP}, #{imagevalue,jdbcType=CLOB})
8   </insert>

mybatis源码已经自动实现了对CLOB字段的查询和插入操作

 

 

二、通过Java代码来查询和插入CLOB字段

 

查询

 

对字段类型进行判断

1 if(obj instanceof String){
2                                     xmlStringEMR = (String)obj;
3                                 } else if(obj instanceof NClob){
4                                     xmlStringEMR = modelMaintainConfigService.clob2Str((NClob)obj);
5                                 }
6                                 else if(obj instanceof Clob){
7                                     xmlStringEMR =oracleClob2Str((Clob) obj);
8                                 }

 

CLOB转成字符串

复制代码
 1 /*
 2      * 将CLOB类型转成String进行解析
 3      * */
 4     public String oracleClob2Str(Clob clob)  {
 5         try {
 6             return (clob != null ? clob.getSubString(1, (int) clob.length()) : null);
 7         } catch (SQLException e) {
 8             // TODO Auto-generated catch block
 9             e.printStackTrace();
10         }
11         return "";
12     }
复制代码

 

将NCLOB转成字符串

复制代码
 1 /**
 2      * 将NCLOB转成字符串
 3      * @param nclob
 4      * @return
 5      * @throws Exception
 6      */
 7     @Override
 8     public String clob2Str(NClob nclob) throws Exception {
 9         String content = "";
10         try {
11             Reader is = nclob.getCharacterStream();
12             BufferedReader buff = new BufferedReader(is);// 得到流
13             String line = buff.readLine();
14             StringBuffer sb = new StringBuffer();
15             while (line != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
16                 sb.append(line);
17                 line = buff.readLine();
18             }
19             content = sb.toString();
20         } catch (Exception e) {
21             log.error("java.sql.NClob类型转java.lang.String类型出错..."+e.getCause());
22             e.printStackTrace();
23         }
24         return content;
25     }
复制代码

 

插入

复制代码
 1 Class.forName("com.mysql.jdbc.Driver");
 2 //new oracle.jdbc.driver.OracleDriver();
 3 //建立连接
 4 conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/myuser", "root", "root");
 5 //使用PreparedStatement对象里来构建并执行SQL语句,7个问号代表7个字段预先要保留的值
 6 pstmt = conn.prepareStatement("INSERT INTO staff(name, age, sex,address, depart, worklen,wage) VALUES (?, ?, ?, ?, ?, ?, ?)");
 7 //通过PreparedStatement对象里的set方法去设置插入的具体数值
 8 pstmt.setString(1, name);
 9 pstmt.setInt(2, age);
10 pstmt.setString(3, sex);
11 pstmt.setString(4,address );
12 pstmt.setString(5, depart);
13 pstmt.setInt(6, worklen);
14 StringReader c = new StringReader(s);
15 //这里插入大字段
16 pstmt.setCharacterStream(7, c,s.length());
17 pstmt.executeUpdate();
复制代码

 

posted on   德邦总管  阅读(779)  评论(0编辑  收藏  举报

编辑推荐:
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示