2013-11-27 134 views
0

图像插入数据库中显示错误。这是我曾尝试将图像插入数据库中

package com.mysql.db.examples; 

import java.io.*; 
import java.sql.*; 

public class BlobInsertTest { 

    public static void main(String a[]){ 

    Connection con = null; 
    PreparedStatement ps = null; 
    InputStream is = null; 
    try { 
     Class.forName("com.mysql.jdbc.Driver"); 
     con = DriverManager. 
       getConnection("jdbc:mysql://localhost:3306/STUDENT_DB","root","sys"); 
     ps = con.prepareStatement("insert into STUDENT_PROFILE values (?,?)"); 
     ps.setInt(1, 101); 
     is = new FileInputStream(new File("D:\\supriyo_pic.JPG")); 
     ps.setBinaryStream(2, is); 
     ps.executeUpdate(); 

    } catch (ClassNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (SQLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (FileNotFoundException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } finally{ 
     try{ 
      if(is != null) is.close(); 
      if(ps != null) ps.close(); 
      if(con != null) con.close(); 
     } catch(Exception ex){} 
    } 
} 
} 

其显示代码:

Exception in thread "main" java.lang.AbstractMethodError: 
    com.mysql.jdbc.ServerPreparedStatement.setBinaryStream(ILjava/io/InputStream;)V 
    at com.mysql.db.examples.BlobInsertTest.main(BlobInsertTest.java:26) 
+1

店面形象路径,而不是像.. – subash

+0

会发生什么,如果您还提供了第三个可选参数:http://www.herongyang.com/JDBC/MySQL-BLOB-setBinaryStream.html

所以,你应该用这个测试setBinaryStream方法? 'is = new FileInputStream(new File(“D:\\ supriyo_pic.JPG”),(int)ps.length());' 我听说有些jdbc驱动程序可能会出现问题,当您不给第三个参数。 – Schnodderbalken

+0

它是什么(int)ps.length())??我无法得到... – pinto

回答

1

根据您的MySQL JDBC驱动程序的版本(它是一个JDBC 3或4类型?),你需要设置setBinaryStream方法的不同参数。

的详细信息可以在这里找到:

... 
File file = new File("D:\\supriyo_pic.JPG"); 
is = new FileInputStream(file); 
ps.setBinaryStream(2, is, (int)file.length()); 
... 
+0

感谢它的工作。 – Braj