要向数据库写入图片,提示"PreparedStatement pst.setBinaryStream(1, isImage,(int)(fImage.length()));"这句出错,我的数据库类型是Blob的,另外,当读取图片时,也是提示"InputStream isImage = rs2.getBinaryStream("MSG_INFO1");"这句出错,请问是什么原因呢?不胜感激啊~~~

解决方案 »

  1.   

    不好意思,忘了把出错信息贴出来:
      java.sql.SQLException: not implemented by SQLite JDBC driver
              at org.sqlite.Unused.unused(Unused.java:29)
              at org.sqlite.Unused.setBinaryStream(Unused.java:58)以上就是出错信息了,谢谢!
      

  2.   

     我用jdbc API写了一个,给你参考一下    package org.chy;import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.InputStream;
    import java.net.URISyntaxException;
    import java.net.URL;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;public class ImaTest {
    public static void insert(File file){
    Connection conn = null;
    PreparedStatement ps = null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://127.0.0.1:3306/test";
    conn = DriverManager.getConnection(url,"root","123");
    String sql = "insert into image values(?)";
    ps = conn.prepareStatement(sql);
    InputStream in = new FileInputStream(file);
    ps.setBinaryStream(1, in,(int) file.length());
    ps.execute();
    }catch(Exception ex){
    ex.printStackTrace();
    }finally{
    try{
    if(ps != null) ps.close();
    if(conn !=null) conn.close();
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    }

    public static void find(){
    Connection conn = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try{
    Class.forName("com.mysql.jdbc.Driver");
    String url = "jdbc:mysql://127.0.0.1:3306/test";
    conn = DriverManager.getConnection(url,"root","123");
    String sql = "select * from image";
    ps = conn.prepareStatement(sql);
    InputStream in = null;
    rs = ps.executeQuery();
    if(rs.next())
    in = rs.getBinaryStream(1);
    FileOutputStream fos = new FileOutputStream("a.gif");
    int data;
    while((data = in.read()) != -1){
    fos.write(data);
    }
    in.close();
    fos.close();
    }catch(Exception ex){
    ex.printStackTrace();
    }finally{
    try{
    if(rs != null) rs.close();
    if(ps != null) ps.close();
    if(conn !=null) conn.close();
    }catch(Exception ex){
    ex.printStackTrace();
    }
    }
    }

    public static void main(String[] args){
    URL url = ClassLoader.getSystemResource("arrow_down.gif");
    File f = null;
    try {
    f = new File(url.toURI());
    } catch (URISyntaxException e) {
    e.printStackTrace();
    }

    //插入
    ImaTest.insert(f);
    //读取
    ImaTest.find();
    }
    }
      

  3.   

      我用jdbc   API写了一个,给你参考一下 
     
            package   org.chy; import   java.io.File; 
    import   java.io.FileInputStream; 
    import   java.io.FileOutputStream; 
    import   java.io.InputStream; 
    import   java.net.URISyntaxException; 
    import   java.net.URL; 
    import   java.sql.Connection; 
    import   java.sql.DriverManager; 
    import   java.sql.PreparedStatement; 
    import   java.sql.ResultSet; public   class   ImaTest   { 
    public   static   void   insert(File   file){ 
    Connection   conn   =   null; 
    PreparedStatement   ps   =   null; 
    try{ 
    Class.forName( "com.mysql.jdbc.Driver "); 
    String   url   =   "jdbc:mysql://127.0.0.1:3306/test "; 
    conn   =   DriverManager.getConnection(url, "root ", "123 "); 
    String   sql   =   "insert   into   image   values(?) "; 
    ps   =   conn.prepareStatement(sql); 
    InputStream   in   =   new   FileInputStream(file); 
    ps.setBinaryStream(1,   in,(int)   file.length()); 
    ps.execute(); 
    }catch(Exception   ex){ 
    ex.printStackTrace(); 
    }finally{ 
    try{ 
    if(ps   !=   null)   ps.close(); 
    if(conn   !=null)   conn.close(); 
    }catch(Exception   ex){ 
    ex.printStackTrace(); 


    } public   static   void   find(){ 
    Connection   conn   =   null; 
    PreparedStatement   ps   =   null; 
    ResultSet   rs   =   null; 
    try{ 
    Class.forName( "com.mysql.jdbc.Driver "); 
    String   url   =   "jdbc:mysql://127.0.0.1:3306/test "; 
    conn   =   DriverManager.getConnection(url, "root ", "123 "); 
    String   sql   =   "select   *   from   image "; 
    ps   =   conn.prepareStatement(sql); 
    InputStream   in   =   null; 
    rs   =   ps.executeQuery(); 
    if(rs.next()) 
    in   =   rs.getBinaryStream(1); 
    FileOutputStream   fos   =   new   FileOutputStream( "a.gif "); 
    int   data; 
    while((data   =   in.read())   !=   -1){ 
    fos.write(data); 

    in.close(); 
    fos.close(); 
    }catch(Exception   ex){ 
    ex.printStackTrace(); 
    }finally{ 
    try{ 
    if(rs   !=   null)   rs.close(); 
    if(ps   !=   null)   ps.close(); 
    if(conn   !=null)   conn.close(); 
    }catch(Exception   ex){ 
    ex.printStackTrace(); 


    } public   static   void   main(String[]   args){ 
    URL   url   =   ClassLoader.getSystemResource( "arrow_down.gif "); 
    File   f   =   null; 
    try   { 
    f   =   new   File(url.toURI()); 
    }   catch   (URISyntaxException   e)   { 
    e.printStackTrace(); 
    } //插入 
    ImaTest.insert(f); 
    //读取 
    ImaTest.find(); 

      

  4.   

    非常感谢!但是我用的是SQLite,不是Mysql,我大致上也是像你说的这样写的,这些语句在连SQL-Server和Oracle的时候都没有问题,但是就偏偏在SQLite出错,上网也找不到解决方法,郁闷啊~~
      

  5.   

    jdbc driver 没有实现setBinaryStream 的方法