Java开发网 Java开发网
注册 | 登录 | 帮助 | 搜索 | 排行榜 | 发帖统计  

您没有登录

» Java开发网 » Database/JDBC/SQL/JDO/Hibernate » MySQL  

按打印兼容模式打印这个话题 打印话题    把这个话题寄给朋友 寄给朋友    该主题的所有更新都将Email到你的邮箱 订阅主题
flat modethreaded modego to previous topicgo to next topicgo to back
作者 Mysql数据库使用Blob类型插入图片的问题!
wallacer

HUST



发贴: 30
积分: 1
于 2006-03-17 16:45 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
今天在实验室整了半天,看Mysql的文档,写了一个测试代码,但是好像插不进去数据啊,不知道问题出在什么地方!很是郁闷!
数据库Mysql5.0
CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(64) NOT NULL,
`img` blob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;

测试代码:

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ImgTest {
  
  private Connection conn = null;
  private Statement stat = null;
  
  public ImgTest() throws Exception {
    Class.forName("org.gjt.mm.mysql.Driver").newInstance();
  }
  
  public void connect(String url, String username, String password) {
    String connect= url + "?" + "user=" + username +
    "&" + "password=" + password;
    try{
      conn= DriverManager.getConnection(connect);
      stat = conn.createStatement();
    }catch(SQLException e){
      System.err.println(e.getLocalizedMessage());
    }    
  }
  
  public void disconnect(){
    try{
      stat.close();
      conn.close();
    }catch(SQLException e){
      System.err.println(e.getLocalizedMessage());
    }
  }
  
  public void insert(String name) throws Exception {
    PreparedStatement pstmt = conn.prepareStatement(
      "insert into test values ( ? , ?, ?)" );
    FileInputStream in = new FileInputStream(name);
    try{
      //System.out.println("0");
      pstmt.setObject(1, null);
      //System.out.println("1");
      pstmt.setObject(2, name);
      //System.out.println("2");
      pstmt.setBinaryStream(3,in,in.available());
      System.out.println("3");
      int re = pstmt.executeUpdate();
      System.out.println(re);
      System.out.println("Successful!");
    }catch(SQLException e){
    }
  }
  
  public ResultSet query() throws Exception {
    return stat.executeQuery("select * from test");
  }
  
  public static void main(String args[]) throws Exception {
    
    String url    = "jdbc:mysql://localhost:3306/test";
    String username = "root";
    String password = "441300";
    
    ImgTest test = null;
    try{
      test = new ImgTest();
    }catch(Exception e){
    }
    test.connect(url,username,password);
    test.insert("Sunset.jpg");
    ResultSet result = test.query();
    /*
    InputStream in = null;
    FileOutputStream out =null;
    String name;
    byte[] b;
    int id;
    try {
      while(result.next()) {
        id  = result.getInt("id");
        name  = result.getString("name").replace("." , "1.");
        in = result.getBinaryStream("img");
        b = new byte[in.available()];
        in.read( b );
        out = new FileOutputStream(new File(name));
        out.write( b );
        String row = id + " " + name;
        System.out.println(row);
      }
      in.close();
      out.close();
      result.close();
      test.disconnect();
    } catch(SQLException e) {}
    */
  }
}


后面的代码有注释,可以在测试的时候去掉!


wallacer edited on 2006-03-17 16:52

 "your future depends on your dream, so go to sleep."
作者 Re:Mysql数据库使用Blob类型插入图片的问题! [Re:wallacer]
wallacer

HUST



发贴: 30
积分: 1
于 2006-03-17 17:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
吃饭后又看了看,发现问题了!在插入的时候我捕获了异常,看不到抛出的异常类型,把try catch注释掉后看到:Data too long for column 'img' at row 1.
不过现在还没有关于blob块到底能有多大的介绍!换了一个很小的gif图片倒是插进去了!



 "your future depends on your dream, so go to sleep."
作者 Re:Mysql数据库使用Blob类型插入图片的问题! [Re:wallacer]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-03-17 18:07 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
Which JDBC driver (and version) are you using?


作者 Re:Mysql数据库使用Blob类型插入图片的问题! [Re:wallacer]
wallacer

HUST



发贴: 30
积分: 1
于 2006-03-17 22:07 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
该问题已经解决了!哈哈


 "your future depends on your dream, so go to sleep."
作者 Re:Mysql数据库使用Blob类型插入图片的问题! [Re:wallacer]
mazalet

如果英俊是一种罪,那

CJSDN高级会员


发贴: 430
积分: 60
于 2006-03-21 14:49 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
怎么解决的呢?


如果你更热爱金钱而非自由,更习惯于被奴役的安宁而畏惧令人充满活力的争取自由的抗争,那么,请你静静地走开。我们不会乞求你的建议或是帮助。伏下身去讨好那喂养你的人吧。但愿身上的锁链不会给你造成太多的痛苦,但愿未来的人们不会记起你曾经是我们的国人。”
---------
作者 Re:Mysql数据库使用Blob类型插入图片的问题! [Re:wallacer]
wallacer

HUST



发贴: 30
积分: 1
于 2006-03-24 21:48 user profilesend a private message to usersearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
将CREATE TABLE `test` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(64) NOT NULL,
`img` blob NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gb2312;
中的`img` blob NOT NULL,行改为:
`img` longblob NOT NULL,



 "your future depends on your dream, so go to sleep."
作者 Re:Mysql数据库使用Blob类型插入图片的问题! [Re:wallacer]
why

問題兒童

总版主


发贴: 4629
积分: 388
于 2006-03-24 23:04 user profilesend a private message to usersend email to whysearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1 point for following up with your own post.



flat modethreaded modego to previous topicgo to next topicgo to back
  已读帖子
  新的帖子
  被删除的帖子
Jump to the top of page

   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923