Demo PreparedStatement setBinaryStream
表结构:
- create table TEST
- (
- ID INTEGER,
- IMG BLOB
- )
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
-
- public class Test {
-
- static {
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
-
-
-
-
-
-
- public static Connection getConnection() {
- Connection conn = null;
- try {
- conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521/orcl", "root", "root");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
-
- public static void main(String[] args) {
-
- PreparedStatement pst = null;
- FileInputStream fis=null;
- File file = new File("d:\\image.jpg");
- Connection conn = getConnection();
- String sql = "insert into test(id,img) values('1',?)";
- try {
- pst = conn.prepareStatement(sql);
- fis=new FileInputStream(file);
- pst.setBinaryStream(1, fis, fis.available());
- pst.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }
-
- }
pst.setBinaryStream(1, fis, fis.available());
三个参数分别为:参数索引,流对象,流对象大小
作者:itmyhome
出处:http://blog.youkuaiyun.com/itmyhome1990/article/details/41824447