码迷,mamicode.com
首页 > 数据库 > 详细

向mysql数据库中插入大二进制文件和大文本

时间:2016-07-08 11:40:46      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

@Test
     public void run() throws ClassNotFoundException, SQLException,
                FileNotFoundException {
 
            try {
                Connection conn = jdbcUtils. getConnection();
                PreparedStatement pst = conn
                           .prepareStatement( "insert into myblob values(null,?)");
                File file = new File( "E:\\Java\\学习\\day17-jdbc\\视频\\1.课程介绍.avi" );
                FileInputStream fs = new FileInputStream(file);
                pst.setBinaryStream(1, fs, ( int) file.length());
                 int row = pst.executeUpdate();
                 if (row != 0) {
                     System. out.println( "插入成功");
                }
           } catch (Exception e) {
                e.printStackTrace();
           }
 
     }
 
 

@Test
public void save() throws ClassNotFoundException, SQLException,
FileNotFoundException {
try {
Connection conn = jdbcUtils.getConnection();
PreparedStatement pst = conn
.prepareStatement("insert into mytext values(null,?)");
File file = new File("E:\\Java\\Workspaces\\DAO\\a.txt");
FileReader fr = new FileReader(file);
pst.setCharacterStream(1, fr, (int) file.length());
int row = pst.executeUpdate();
if (row != 0) {
System.out.println("插入成功");
}
} catch (Exception e) {
e.printStackTrace();
}

}

向mysql数据库中插入大二进制文件和大文本

标签:

原文地址:http://www.cnblogs.com/haofaner/p/5652755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!