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

发布时间:2020-04-01 22:48:12 作者:tozgb
来源:网络 阅读:433

将开发过程中经常用的一些内容片段做个珍藏,下面资料是关于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();
}

}

推荐阅读:
  1. 【代码】c++堆的简单实现
  2. 关于java面向对象的三大特性介绍

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java 代码 ava

上一篇:C++函数模板举例

下一篇:winds环境开发ios软件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》