设为首页 加入收藏

TOP

clob保存为本地xml文件,修改后上传(一)
2015-07-24 10:24:02 来源: 作者: 【 】 浏览:0
Tags:clob 保存 本地 xml 文件 修改 上传

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

首先将数据库的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 = null;
		Connection conn = DB.getConn();
		Statement stmt = DB.createStmt(conn);
		try {
			conn.setAutoCommit(false);
		} catch (SQLException e1) {
			e1.printStackTrace();
		}

		String sql1 = "update BD_PROCESS_DEF_VER s set s.NODE_INFO=' ' where ID="
				+ id; // 这边需要设置一个空的字段,后面就不会出现空指针
		try {
			stmt.execute(sql1);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String sql2 = "select * from BD_PROCESS_DEF_VER s where s.ID=" + id;
		// 锁定数据行进行更新,注意“for update”语句
		ResultSet rs = null;
		try {
			rs = stmt.executeQuery(sql2);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		try {
			while (rs.next()) {
				String sql3 = "update BD_PROCESS_DEF_VER set NODE_INFO= ? where ID="+ id;
				pst = conn.prepareStatement(sql3);
				pst.setCharacterStream(1, rd, 100000000);
				pst.executeUpdate();
			}
			// 最后一步自己提交
			conn.commit();
			conn.setAutoCommit(true);

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DB.close(rs);
			DB.close(stmt);
			try {
				pst.close(
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据块内部偏移量的基本计算方法 下一篇sumover用法,以及与groupby的区别

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·微服务 Spring Boot (2025-12-26 18:20:10)
·如何调整 Redis 内存 (2025-12-26 18:20:07)
·MySQL 数据类型:从 (2025-12-26 18:20:03)
·Linux Shell脚本教程 (2025-12-26 17:51:10)
·Qt教程,Qt5编程入门 (2025-12-26 17:51:07)