java之操作MySql操作大对象

1.数据库表结构如下

  

  这里数据类型根据插入数据的大小进行选择。

1.具体实现如下,这里主要将图片转为二进制存入数据库

 1 Connection conn=null;
 2         PreparedStatement st=null;
 3         String sql="insert into users(username,password,regdate,img) values(?,?,?,?)";
 4         try {
 5             if(conn==null){
 6                 conn = db.getConnection();
 7             }
 8             st = conn.prepareStatement(sql);
 9             File file=new File(this.getClass().getClassLoader().getResource("../../").getPath()+"1.jpeg");
10             InputStream input=new BufferedInputStream(new FileInputStream(file));
11             st.setString(1,user.getUsername());
12             st.setString(2, user.getPassword());
13             st.setDate(3, new Date(user.getRegdate().getTime()));
14             st.setBinaryStream(4, input,(int)file.length());//这里需要注意,后面的file.length()数据类型为long,在这里需要将其强转为int类型
15             int result=st.executeUpdate();             
16             if(result>0){
17                 return true;
18             }
19         } catch (SQLException e) {
20             e.printStackTrace();
21         } catch (FileNotFoundException e) {
22             e.printStackTrace();
23         } finally{
24             if(st!=null)
25             db.close(st);
26             if(conn!=null)
27             db.close(conn);
28         }
29         return false;

3.注意:

  PreparedStatement对象中的setBinaryStream(int index,inputstream input,long)方法在jdk1.6存在,但是在mysql的jdbc中并没有实现,如果后面的文件长度没有转换为int的话则会报以下错误

java.lang.AbstractMethodError: com.mysql.jdbc.PreparedStatement.setBinaryStream(ILjava/io/InputStream;J)V

4.从数据库中取出二进制文件

 

 1 Connection conn = null;
 2         PreparedStatement st = null;
 3         String sql = "select * from users where id=?";
 4         try {
 5             if (conn == null) {
 6                 conn = JdbcUtil.getConnection();
 7             }
 8             st = conn.prepareStatement(sql);
 9             st.setInt(1, user.getId());
10             File file = new File(this.getClass().getClassLoader().getResource("../../").getPath()+ "back.jpeg");
11             OutputStream output = new BufferedOutputStream(new FileOutputStream(file));
12             ResultSet result = st.executeQuery();
13             while (result.next()) {
14                 byte[] by = new byte[1024];
15                 int i = 0;
16                 InputStream input = result.getBinaryStream(5);//取出二进制数据    
17                 try {
18                     while ((i = input.read(by)) != -1)
19                         output.write(by, 0, i);//通过输出流写到文件中  
20                 } catch (IOException e) {
21                     e.printStackTrace();
22                 }
23 
24             }
25 
26         } catch (SQLException e) {
27             e.printStackTrace();
28         } catch (FileNotFoundException e) {
29             e.printStackTrace();
30         } finally {
31             if (st != null)
32                 JdbcUtil.close(st);
33             if (conn != null)
34                 JdbcUtil.close(conn);
35         }
36         return false;

 

posted @ 2015-04-22 18:28  jamesmichel  阅读(537)  评论(0编辑  收藏  举报