clob保存为本地xml文件,修改后上传

这篇博客介绍了如何使用Java和JDBC处理CLOB类型数据,具体步骤包括将数据库中的CLOB内容保存为UTF-8格式的XML文件,对XML文件进行编辑,然后将修改后的XML内容重新上传到数据库。主要关注点在于文件的编码格式和XML节点的修改。
摘要由CSDN通过智能技术生成

这两天与小伙伴写了一个小程序,实现的功能如下:

首先将数据库的clob保存为本地的xml文件,然后对xml进行修改后上传至数据库

主要的难点如下:

1:clob文件的下载与上传,其中保存为本地的文件要求是UTF-8格式 

2:xml文件中节点的修改


clob的上传与下载如下

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.sql.Clob;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @author GuoDi-CT DC
 *
 */

public class ClobModify {

	/**
	 *@param id 数据库中的ID
	 * 返回 保存文件的绝对路径
	 */
	public static String ClobToXml(int id) {
		Connection conn = DB.getConn();
		Statement stmt = DB.createStmt(conn);
		String sql = "select * from BD_PROCESS_DEF_VER where ID =" + id;
		ResultSet rs = DB.getRs(stmt, sql);
		String xmlFile = null;

		try {
			if (rs.next()) {
				int fjbh = rs.getInt(1);
				xmlFile = "d:\\xml\\" + fjbh + ".xml";
				Clob clob = rs.getClob(16);
				FileOutputStream fo = new FileOutputStream(xmlFile);
				OutputStreamWriter so = new OutputStreamWriter(fo, "UTF-8");
				if (clob != null) {
					Reader is = clob.getCharacterStream();
					BufferedReader br = new BufferedReader(is);
					String s = br.readLine();
					while (s != null) {
						so.write(s + System.getProperty("line.separator"));
// System.out.println(str);
						s = br.readLine();
					}
				}
				so.flush();
				so.close();
			}
		} catch (SQLException | IOException e) {
			e.printStackTrace();
		}
		DB.close(rs);
		DB.close(stmt);
		DB.close(conn);
		return xmlFile;
	}

	public static void updateClob(String fileName, int id) {

		FileInputStream fis = null;
		InputStreamReader rd = null;
		
		try {
			fis = new FileInputStream(fileName);
			rd = new InputStreamReader(fis, "UTF-8");
		} catch (FileNotFoundException e2) {
			e2.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		PreparedStatement pst &
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值