传输数据压缩实例

为了更好的的演示传输数据压缩功能,我们采用数据量较大的Blob做为演示对象,下面是传输数据压缩的实例代码:

package com.sun.cts.tests.jdbc.help_doc.shi_li;
/**************************************************
 *这个例子演示传输数据压缩功能
 *
 *新的文档本示例被删除
 **************************************************/
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import com.oscar.util.StreamHandle;

public class TransferCompressDemo {

	/**
	 * 测试数据量:接近
	 */
	protected static final int TEST_DATA_LENGTH = 10000000;
	private Connection con;
	Statement stmt;
	public TransferCompressDemo() {
		try {
			Class.forName("com.oscar.Driver");
		} catch (java.lang.ClassNotFoundException e) {
			e.printStackTrace();
		}
		connect();
		testTransferCompress();
	}

	public void connect() {
		String url = "jdbc:oscar://localhost:2003/osrdb";
		String name = "sysdba";
		String password = "szoscar55";
		Properties p = new Properties();
		p.setProperty("user", name);
		p.setProperty("password", password);
		//废弃
		//p.setProperty("compressTransfer", "true");
		try {
			con = DriverManager.getConnection(url, p);
		} catch (SQLException se) {
			System.out.print(se);
		}
	}

	public void testTransferCompress() {
		ResultSet rs = null;
		PreparedStatement ps = null;
		FileOutputStream fos1 = null;
		FileOutputStream fos2 = null;
		try {
			stmt = con.createStatement();
			byte[] inBytes = null;
			ByteArrayOutputStream bos = new ByteArrayOutputStream(TEST_DATA_LENGTH);
			for (int i = 0; i < TEST_DATA_LENGTH; i++) {
				bos.write(i % 0xff);
			}
			inBytes = bos.toByteArray();
			byte[] outBytes = null;
			/* 创建表 */
			String sql = "CREATE TABLE TEST_BLOB(ID INT,FILE BLOB)";
			stmt.execute(sql);
			/*插入Blob数据*/
			ps = con.prepareStatement("insert into TEST_BLOB values (?, ?) ");
			ps.setInt(1, 2);
			ps.setBytes(2, inBytes);
			ps.execute();

			/* 查询插入的大对象数据 */
			stmt.execute("select * from TEST_BLOB");
			rs = stmt.getResultSet();
			if (rs.next()) {
				System.out.println(rs.getInt(1));
				outBytes = rs.getBytes(2);
			}
			/*
			 * 将输入数据和数据库查询结果数据均输出到文件中,
			 *  判断输出结果是否正确
			 */
			fos1 = new FileOutputStream("test/insertBlob_org.dat");
			fos2 = new FileOutputStream("test/outBlob_query.dat");
			/*
			 * 将inBytes的全部数据输出到fos1中,
			 *  输出时使用100000大小的分片
			 */
			StreamHandle.write(fos1, inBytes, 0, -1, 100000);
			/*
			 * 将outBytes的全部数据输出到fos2中,
			 *  输出时使用100000大小的分片
			 */
			StreamHandle.write(fos2, outBytes, 0, -1, 100000);
			
			//删除表
			sql="DROP TABLE TEST_BLOB ";
			stmt.executeUpdate(sql);
		} catch (Exception ex) {
			ex.printStackTrace();
		}finally {
			try {
				if (rs!=null) {
					rs.close();
				}
				if (ps!=null) {
					ps.close();
				}
				if (fos1!=null) {
					fos1.close();
				}
				if (fos2!=null) {
					fos2.close();
				}
				if (stmt!=null) {
					stmt.close();
				}
				if (con!=null) {
					con.close();
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new TransferCompressDemo();
	}

}