JDBC从文件中读取内容写入CLOB字段的例子
2007-06-19 11:09
423 查看
public void insertClob() throws Exception {
String content = String.valueOf("");
try {
BufferedReader in = new BufferedReader(
new FileReader("c:/config.xml"));
String str;
while ((str = in.readLine()) != null) {
content = content.concat(str);
}
in.close();
} catch (IOException e) {
}
Statement statement = null;
ResultSet resultset = null;
Connection connection = null;
try {
String user = "ahcwtest";
String password = "ahcwtest";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.130.245:1521:orcl";
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, user, password);
// 设置不自动提交
connection.setAutoCommit(false);
// 创建数据库操作语句
statement = connection.createStatement();
// 定义SQL语句
statement.executeUpdate("update A set B = EMPTY_CLOB()");
String strSQL = "SELECT B FROM A WHERE ROWNUM = 1 FOR UPDATE";
resultset = statement.executeQuery(strSQL);
CLOB contents = null;
while (resultset.next()) {
// 取出CLOB对象
contents = (oracle.sql.CLOB) resultset.getClob(1);
}
Writer out = contents.getCharacterOutputStream();
out.write(content);
out.flush();
out.close();
// 数据库提交
connection.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
-----------------------------------
读取clob的内容到一个文件中
public void readClob() throws Exception {
String content = String.valueOf("");
// try {
// BufferedReader in = new BufferedReader(
// new FileReader("c:/test.dat"));
// String str;
// while ((str = in.readLine()) != null) {
// content = content.concat(str);
// }
// in.close();
// } catch (IOException e) {
// }
Statement statement = null;
ResultSet resultset = null;
Connection connection = null;
try {
String user = "ahcwtest";
String password = "ahcwtest";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.130.245:1521:orcl";
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, user, password);
// 设置不自动提交
connection.setAutoCommit(false);
// 创建数据库操作语句
statement = connection.createStatement();
// 定义SQL语句
String strSQL = "SELECT B FROM A WHERE ROWNUM = 1";
resultset = statement.executeQuery(strSQL);
oracle.sql.CLOB contents = null;
while (resultset.next()) {
// 取出CLOB对象
contents = (oracle.sql.CLOB) resultset.getClob("B");
}
BufferedReader a = new BufferedReader(contents.getCharacterStream());
String str = "";
while ((str = a.readLine()) != null) {
content = content.concat(str);
}
BufferedWriter out = new BufferedWriter(new FileWriter("C:/tttt.txt"));
out.write(content);
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
statement.close();
connection.close();
}
}
String content = String.valueOf("");
try {
BufferedReader in = new BufferedReader(
new FileReader("c:/config.xml"));
String str;
while ((str = in.readLine()) != null) {
content = content.concat(str);
}
in.close();
} catch (IOException e) {
}
Statement statement = null;
ResultSet resultset = null;
Connection connection = null;
try {
String user = "ahcwtest";
String password = "ahcwtest";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.130.245:1521:orcl";
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, user, password);
// 设置不自动提交
connection.setAutoCommit(false);
// 创建数据库操作语句
statement = connection.createStatement();
// 定义SQL语句
statement.executeUpdate("update A set B = EMPTY_CLOB()");
String strSQL = "SELECT B FROM A WHERE ROWNUM = 1 FOR UPDATE";
resultset = statement.executeQuery(strSQL);
CLOB contents = null;
while (resultset.next()) {
// 取出CLOB对象
contents = (oracle.sql.CLOB) resultset.getClob(1);
}
Writer out = contents.getCharacterOutputStream();
out.write(content);
out.flush();
out.close();
// 数据库提交
connection.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
-----------------------------------
读取clob的内容到一个文件中
public void readClob() throws Exception {
String content = String.valueOf("");
// try {
// BufferedReader in = new BufferedReader(
// new FileReader("c:/test.dat"));
// String str;
// while ((str = in.readLine()) != null) {
// content = content.concat(str);
// }
// in.close();
// } catch (IOException e) {
// }
Statement statement = null;
ResultSet resultset = null;
Connection connection = null;
try {
String user = "ahcwtest";
String password = "ahcwtest";
String driver = "oracle.jdbc.driver.OracleDriver";
String url = "jdbc:oracle:thin:@192.168.130.245:1521:orcl";
Class.forName(driver).newInstance();
connection = DriverManager.getConnection(url, user, password);
// 设置不自动提交
connection.setAutoCommit(false);
// 创建数据库操作语句
statement = connection.createStatement();
// 定义SQL语句
String strSQL = "SELECT B FROM A WHERE ROWNUM = 1";
resultset = statement.executeQuery(strSQL);
oracle.sql.CLOB contents = null;
while (resultset.next()) {
// 取出CLOB对象
contents = (oracle.sql.CLOB) resultset.getClob("B");
}
BufferedReader a = new BufferedReader(contents.getCharacterStream());
String str = "";
while ((str = a.readLine()) != null) {
content = content.concat(str);
}
BufferedWriter out = new BufferedWriter(new FileWriter("C:/tttt.txt"));
out.write(content);
out.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
statement.close();
connection.close();
}
}
相关文章推荐
- clob字段只能写入4k内容,超过后报null at org.apache.openjpa.jdbc.sql.OracleDictionary.putString
- C# 从内嵌资源读取文件内容并写入到指定位置
- Learning Python 013 按行读取文件(逐行读取) --- 按行写入文件(逐行写入) --- 实战:从字幕文件中提取字幕内容
- java按指定编码写入和读取文件内容
- 读取mysql文件,写入csv,带字段名
- 读取文本的内容和将文本写入文件中
- 遍历读取文件夹中.txt文件,并将内容写入.txt文件
- 从Excel中读取内容写入txt文件中
- python读取 .txt 文本内容以及将程序执行结果写入txt文件
- awk循环读取文件内容,并把字段赋给相应的变量
- iphone读取和写入文件内容--仅供学习
- 读取写入文件内容
- 从文件读取内容,删除一个数据,把删除后的数据重新写入到文件
- c++写入oracle数据库blob字段,读取blob到本地文件
- 【Node.js】'readline' 逐行读取、写入文件内容
- Scala文件的读取和写入 从控制台输入内容
- Scala文件的读取与写入,从控制台输入内容
- java按指定编码写入和读取文件内容的类分享
- JAVA 创建TXT文件,写入文件内容,读取文件内容
- NET(C#)中将DataTable内容写入到CSV文件 与 将CSV文件内容读取到DataTable中(原创)