如何将图像上传到数据库

时间:2015-03-13 11:41:21

标签: java database blob

这是我的代码

    Connection con = null;
    PreparedStatement ps = null;
    InputStream is = null;
    try {
        Class.forName("oracle.jdbc.driver.OracleDriver");
        con = DriverManager.
                getConnection("jdbc:oracle:thin:@<hostname>:<port num>:<DB name>"
                    ,"user","password");
        ps = con.prepareCall("insert into student_profile values (?,?)");
        ps.setInt(1, 101);
        is = new FileInputStream(new File("Student_img.jpg"));
        ps.setBinaryStream(2, is);
        int count = ps.executeUpdate();
        System.out.println("Count: "+count);
    } 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){}
    }
}

}

我不知道我错在哪里,但执行代码时出错 我得到的例外

  • 线程“main”中的异常java.lang.AbstractMethodError:方法oracle / jdbc / driver / OraclePreparedStatementWrapper.setBlob(ILjava / io / InputStream;)V是oracle.jdbc.driver.OraclePreparedStatementWrapper.setBlob(OraclePreparedStatementWrapper.java)中的抽象)

3 个答案:

答案 0 :(得分:0)

请不要投票,我没有50个声誉来添加评论。只需用我的答案指出问题,我最终会将其删除。

我不知道它是否是正确答案,但在我的情况下,我不得不切换到oracle驱动程序版本6.以前的驱动程序有Blob问题。在maven:

<dependency>
    <groupId>com.oracle</groupId>
    <artifactId>oracle-jdbc</artifactId>
    <version>6</version>
</dependency>

答案 1 :(得分:0)

使用get getBlob()方法使用ResultSet从数据库中获取图像。

ResultSet rs = null;
    PreparedStatement pstmt = null;
    String query = "SELECT photo FROM MyPictures WHERE id = ?";
    try {
      pstmt = conn.prepareStatement(query);
      pstmt.setInt(1, id);
      rs = pstmt.executeQuery();
      rs.next();
      Blob blob = rs.getBlob("photo");
      // materialize BLOB onto client
      return blob.getBytes(1, (int) blob.length());
    } finally {
      rs.close();
      pstmt.close();
      conn.close();
    }

Code Source

Insert BLOG(Picture or Photo) Data Type Into Oracle Database

答案 2 :(得分:0)

使用setBinaryStream()方法

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class InsertPictureToMySql {
public static void main(String[] args) throws Exception,    IOException, SQLException {
Class.forName("org.gjt.mm.mysql.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/databaseName", "root", "root");
String INSERT_PICTURE = "insert into MyPictures(id, name, photo) values (?, ?, ?)";

FileInputStream fis = null;
PreparedStatement ps = null;
try {
  conn.setAutoCommit(false);
  File file = new File("myPhoto.png");
  fis = new FileInputStream(file);
  ps = conn.prepareStatement(INSERT_PICTURE);
  ps.setString(1, "001");
  ps.setString(2, "name");
  ps.setBinaryStream(3, fis, (int) file.length());
  ps.executeUpdate();
  conn.commit();
} finally {
  ps.close();
  fis.close();
}
}
}