• 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 为方便开心洋葱网用户,开心洋葱官网已经开启复制功能!
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!
  • 由于近期流量激增,小站的ECS没能经的起亲们的访问,本站依然没有盈利,如果各位看如果觉着文字不错,还请看官给小站打个赏~~~~~~~~~~~~~!

java大对象存取的简单实现

Mysql 水墨上仙 2467次浏览

java大对象存取的简单实现
代码出自: http://www.itlanguageexpress.info/?p=237

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;
/**
 * 这里不仅仅是word文件,所有大对象文件都适用
 * @author Administrator
 *
 */
public class MySqlLargeObjectAccess {
    public static String DRIVER = "org.gjt.mm.mysql.Driver";
    // JDBC:mysql://localhost|IP:端口号/数据库名
    public static String URL = "JDBC:mysql://localhost:3306/JustTest";
    // SERVER NAME
    public static String USER = "root";
    // PASS WORD
    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) {
            }
        }
    }
    /**
     * 存word
     * @param conn
     * @throws SQLException
     * @throws IOException
     */
    public static void insertWordFileToMySql(Connection conn)
            throws SQLException, IOException {
        PreparedStatement pstm = conn
                .prepareStatement("insert into wordInputTest values (?,?)");
        // fileName
        pstm.setString(1, "docx1");
        InputStream in = new FileInputStream("F:" + File.separator
                + "test1.docx");
        // file
        pstm.setBinaryStream(2, in, in.available());
        pstm.executeUpdate();
        pstm.close();
    }
    /**
     * 取word
     * @param conn
     * @throws SQLException
     * @throws IOException
     */
    public static void fetchWordFileFromMySql(Connection conn)
            throws SQLException, IOException {
        PreparedStatement pstm = conn
                .prepareStatement("select * from wordInputTest where fileName = ?");
        pstm.setString(1, "docx1");
        ResultSet rs = pstm.executeQuery();
        int count = 1;
        while(rs.next()) {
            //file为存储文件列的name
            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();
    }
}


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明java大对象存取的简单实现
喜欢 (0)
加载中……