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中的字符串对象都保存在字符串常量池 ...
随机推荐
- Pyhton网络爬虫实例_豆瓣电影排行榜_BeautifulSoup4方法爬取
-----------------------------------------------------------学无止境------------------------------------- ...
- [JSON].remove( keyPath )
语法:[JSON].remove( keyPath ) 返回:无 说明:移除指定路径的键 示例: Set jsonObj = toJson("{div:{'#text-1': 'is tex ...
- linux学习总结----mongoDB总结
dbhelper.py 用户登录和注册(加密算法) 加密导包 import hashlib 或者使用Md5 加密 MongoDB ->JSON service mysql start servi ...
- stm32之SPI通信协议
SPI (Serial Peripheral interface),顾名思义就是串行外围设备接口.SPI是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚,同时为P ...
- JDK源码分析:Object.java
一. 序言 Object.java是一切类的基类,所以了解该类有一定的必要 二 .属性及方法分析 方法列表: private static native void registerNatives(); ...
- Java中二进制数与整型之间的转换
import java.io.*; public class Test{ /** * 二进制与整型之间的转换 * @param args * @throws IOException */ public ...
- python常用命令—查看模块所在位置
环境:ipython3 交互式解释器 语法: import 模块名 模块名.__file__ 功能: 查看模块的所在位置 例:
- POJ 3308 Paratroopers(最大流最小割の最小点权覆盖)
Description It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the ...
- js如何使浏览器允许脚本异步加载
js如何使浏览器允许脚本异步加载 如果脚本体积很大,下载和执行的时间就会很长,因此造成浏览器堵塞,用户会感觉到浏览器“卡死”了,没有任何响应.这显然是很不好的体验,所以浏览器允许脚本异步加载,下面就是 ...
- iOS开发应用程序更新
#import "ViewController.h" //1一定要先配置自己项目在商店的APPID,配置完最好在真机上运行才能看到完全效果哦 #define STOREAPPID ...