java大对象存取的简单实现的代码

将开发过程中经常用的一些内容片段做个珍藏,下面资料是关于java大对象存取的简单实现的内容

package info.itlanguageexpress.LargeObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.sqlException;
public class MySqlLargeObjectAccess {

public static String DRIVER = "org.gjt.mm.MysqL.Driver";
public static String USER = "root";
public static String PASSWORD = "your password";

public static void main(String[] args) throws Exception {
    Connection conn = null;
    try {
        Class.forName(DRIVER);
        conn = DriverManager.getConnection(URL, USER, PASSWORD);
        insertWordFiletoMysqL(conn);
        fetchWordFileFromMysqL(conn);
    } catch (Exception ex) {
        ex.printstacktrace(System.out);
    } finally {
        try {
            conn.close();
        } catch (Exception ex) {
        }
    }
}
public static void insertWordFiletoMysqL(Connection conn)
        throws sqlException, IOException {
    PreparedStatement pstm = conn
            .prepareStatement("insert into wordInputTest values (?,?)");
    pstm.setString(1, "docx1");
    InputStream in = new FileInputStream("F:" + File.separator
            + "test1.docx");
    pstm.setBinaryStream(2, in, in.available());
    pstm.executeUpdate();
    pstm.close();
}
public static void fetchWordFileFromMysqL(Connection conn)
        throws sqlException, IOException {
    PreparedStatement pstm = conn
    pstm.setString(1, "docx1");
    ResultSet rs = pstm.executeQuery();
    int count = 1;
    while(rs.next()) {
        InputStream in = rs.getBinaryStream("file");
        FileOutputStream out = new FileOutputStream("F:" + File.separator + "testcopy" + count + ".docx");
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
            out.flush();
        }
        out.close();
        in.close();
        count++;
    }
    rs.close();
    pstm.close();
}

}

相关文章

今天小编给大家分享一下excel图案样式如何设置的相关知识点,...
这篇文章主要讲解了“win10设置过的壁纸如何删除”,文中的讲...
这篇“Xmanager怎么显示远程linux程序的图像”文章的知识点大...
今天小编给大家分享一下xmanager怎么连接linux的相关知识点,...
这篇“如何重置Linux云服务器的远程密码”文章的知识点大部分...
本篇内容介绍了“Linux云服务器手动配置DNS的方法是什么”的...