java oracle clob string 大字符串存储【转】
单位用到了oracle存储string类型到数据库里的clob,上网查看资料找到解决方案。如下:
public class ClobTest { static String url = "jdbc:oracle:thin:@10.12.10.18:1521:orcl";
static String user = "cwbe1_9999";
static String pwd = "or777";
static String text = "这是要插入到CLOB里面的数据,更新数据!" + "update"; private static int clobImport() throws ClassNotFoundException, SQLException {
DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象
String sql = "insert into ETLNEEDPARAM(F_KEY,F_VALUE) values ('defaultpo',?)";// 要执行的SQL语句
PreparedStatement stmt = conn.prepareStatement(sql);// 加载SQL语句
// PreparedStatement支持SQL带有问号?,可以动态替换?的内容。
Reader clobReader = new StringReader(text); // 将 text转成流形式
stmt.setCharacterStream(1, clobReader, text.length());// 替换sql语句中的?
int num = stmt.executeUpdate();// 执行SQL
if (num > 0) {
System.out.println("ok");
} else {
System.out.println("NO");
}
stmt.close();
conn.close();
return num;
} private static int clobUpdate(String key) throws ClassNotFoundException, SQLException { DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象
String sql = "update ETLNEEDPARAM set F_VALUE = ? where F_KEY = ? ";// 要执行的SQL语句
PreparedStatement stmt = conn.prepareStatement(sql);// 加载SQL语句
// PreparedStatement支持SQL带有问号?,可以动态替换?的内容。
Reader clobReader = new StringReader(text); // 将 text转成流形式
stmt.setString(2, key);
stmt.setCharacterStream(1, clobReader, text.length());// 替换sql语句中的?
int num = stmt.executeUpdate();// 执行SQL
if (num > 0) {
System.out.println("ok");
} else {
System.out.println("NO");
}
stmt.close();
conn.close();
return num;
} private static String clobExport() throws ClassNotFoundException, SQLException, IOException { CLOB clob = null;
String sql = "select F_VALUE from ETLNEEDPARAM where F_KEY ='test1'";
DriverManager.registerDriver(new OracleDriver());
Connection conn = DriverManager.getConnection(url, user, pwd);// 得到连接对象
PreparedStatement stmt = conn.prepareStatement(sql);
ResultSet rs = stmt.executeQuery();
String content = "";
if (rs.next()) {
clob = (oracle.sql.CLOB) rs.getClob("F_VALUE"); // 获得CLOB字段str
// 注释: 用 rs.getString("str")无法得到 数据 ,返回的 是 NULL;
content = ClobToString(clob);
}
stmt.close();
conn.close();
return content;
} // 将字CLOB转成STRING类型 public static 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();
// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
while (s != null) {
sb.append(s);
s = br.readLine();
}
reString = sb.toString();
return reString;
} public static void main(String[] args) throws IOException,ClassNotFoundException, SQLException {
// System.out.println(clobImport());
System.out.println(clobUpdate("fmo"));
System.out.println(clobUpdate("epo")); // System.out.println(clobExport()); }
转自:http://bestxiaok.iteye.com/blog/1027733
java oracle clob string 大字符串存储【转】的更多相关文章
- 为什么 char 数组比 Java 中的 String 更适合存储密码?
另一个基于 String 的棘手 Java 问题,相信我只有很少的 Java 程序员可以正确回答这个问题.这是一个真正艰难的核心Java面试问题,并且需要对 String 的扎实知识才能回答这个问题. ...
- Java SE之String,字符串和子字符串的存储与区别
理解String 是怎么占用内存的 来看一个每个String对象的各个属性,一个String包括如下的属性: 一个char数组(是个独立的对象用来存储字符串中的字符) 一个int 的off ...
- [java] StringBuilder / StringBuffer / String 建立字符串
1.三者在建立新字符串速度方面的比较: StringBuilder > StringBuffer > String 2.线程安全性: StringBuilder:线程非安全的 Str ...
- 通过Java编码获取String分行字符串的内容
代码案列: import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.IOException ...
- 看结果,测试?java中的String类 字符串拆分成字符串数组 判定邮箱地址 字符串比较 参数传递?
看结果1? package com.swift; class ArrayString { public static void main(String[] args) { String str = & ...
- java读取clob字段的几种方法
http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece7631046893b4c4380143fd3d1027fa3c215cc790f1a06 ...
- java读取clob字段的几种方法(转)
http://blog.csdn.net/tanksyg/article/details/49927897 第一种 Clob clob = rs.getClob("remark") ...
- Java 读取clob字段的几种方法
Java 读取clob字段的几种方法 一.第一种 Clob clob = rs.getClob("remark");//Java.sql.Clob String detailinf ...
- Java为什么把String设计成不可变的(immutable)
在java中,String是字符串常量,可以从内存,同步机制,数据结构等方面分析 1:字符串中常量池的需要 String不同于普通基础变量类型的地方在于对象.java中的字符串对象都保存在字符串常量池 ...
随机推荐
- Python 多线程、进程、协程上手体验
浅谈 Python 多线程.进程.协程上手体验 前言:浅谈 Python 很多人都认为 Python 的多线程是垃圾(GIL 说这锅甩不掉啊~):本章节主要给你体验下 Python 的两个库 Thre ...
- 打印队列 (Printer Queue,ACM/ICPC NWERC 2006,UVA12100)
题目描述: 题目思路: 使用一个队列记录数字,一个优先队列记录优先级,如果相等即可打印: #include <iostream> #include <queue> using ...
- 数据库Mysql的学习(三)-各种约束
删除数据库表 drop table [if exists] 表一,表二.....; 表分区:比如图书信息表有1000万个图书信息,如何优化他,其中一种方式就是表分区.就是把一张表的数据分成多个区块,这 ...
- 浅谈JS-cookie,你是香甜可口的小点心吗?
引言: 想必大家一定听过或看过浏览器cookie,早在nokia雄霸天下.我们还不太明白浏览器的时候,cookie就已经悄悄地存在于浏览器的“设置选项”中了.当时它的用途仅仅是让你选择是否“清除”.年 ...
- Python3 Tkinter-Radionbutton
1.创建单选按钮 from tkinter import * root=Tk() Radiobutton(root,text='b1').pack() Radiobutton(root,text='b ...
- BZOJ 3924 ZJOI2015 幻想乡战略游戏 树链剖分
题目链接:https://www.luogu.org/problemnew/show/P3345(bzoj权限题) 题意概述:动态维护树的上所有点到这棵树的带权重心的距离和.N,Q<=10000 ...
- SPOJ 694 Distinct Substrings/SPOJ 705 New Distinct Substrings(后缀数组)
Given a string, we need to find the total number of its distinct substrings. Input T- number of test ...
- POJ 1921 Paper Cut(计算几何の折纸问题)
Description Still remember those games we played in our childhood? Folding and cutting paper must be ...
- str和repr
在Python2.6和Python3.0以及更早的版本中,在交互式模式下的输出本质上是使用repr,因此对于一些浮点数运算,会显示很多位: 4 / 5.0 #0.8000000000000004 但是 ...
- Java中的死锁问题
死锁问题: 例如有两个线程, 线程1与线程2. 线程1在执行的过程中, 要锁定对象1, 2才能完成整个操作, 首先锁定对象1, 再锁定对象2. 线程2在执行的过程中, 要锁定对象2, 1才能完成整个操 ...