论坛首页 综合技术论坛

Oracle之CLOB操作

浏览 1647 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (11) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-04-10  

package com;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;

import oracle.jdbc.driver.OraclePreparedStatement;
/*
* Oracle 10g对CLOB类型的操作进行了相当程度的简化,
* 一般情况下(不超过32,765字节), 使用新版的ojdbc14.jar驱动,
* 就可以在Java代码中和VARCHAR2类型一样用getString和setString进行读写操作,
* 这给开发带来了很大的便利. 不过当超过32,765字节又该如何处理呢? 这就需要用到
* OraclePreparedStatement
*/
public class Test {
public static void main(String[] args) {
String str1 = getStr(20, "a") + "中文";
String str2 = getStr(3996, "a") + "中文";
String str3 = getStr(32761, "a") + "中文";
String str4 = "b"+getStr(42761, "a") + "中文";

String driver = "oracle.jdbc.OracleDriver";
String url = "jdbc:oracle:thin:@localhost:1521:WEB";
String uid = "system";
String pwd = "web";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url,uid,pwd);
//String sql = "create table t(tid number,ttext clob)";
//conn.createStatement().execute(sql);
String i1 = "insert into t values(?,?)";
//PreparedStatement psmt = conn.prepareStatement(i1);

OraclePreparedStatement opsmt = (OraclePreparedStatement)conn.prepareStatement(i1);
opsmt.setInt(1,2);
opsmt.setStringForClob(2,str4);
opsmt.executeUpdate();

//String i2 = "insert into t values(8,'"+str2+"')";
//conn.createStatement().executeUpdate(i2);


ResultSet rs = conn.createStatement().executeQuery("select * from t order by tid desc");
while(rs.next()){
System.out.print(6);
System.out.println(rs.getString(2));
}
conn.commit();
} catch (Exception e) {
e.printStackTrace();
}

// System.out.println(str2);
// System.out.println(str2.length());
}

static String getStr(int n, String type) {
String str = "";
for (int i = 0; i < n; i++) {
str += type;
}
return str;
}

}

论坛首页 综合技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics