Java PreparedStatement .setBinaryStream (int parameterIndex, InputStream x, int length)

Syntax

PreparedStatement.setBinaryStream(int parameterIndex, InputStream x, int length) has the following syntax.

void setBinaryStream(int parameterIndex,   InputStream x,   int length)   throws SQLException

Example

In the following code shows how to use PreparedStatement.setBinaryStream(int parameterIndex, InputStream x, int length) method.


import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
/*from   w w  w .  ja  va 2 s  .c  om*/
public class Main {
  public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (Id int, b BLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");    

    File file = new File("blob.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setBinaryStream(1, fis, (int) file.length());
    pstmt.execute();
    
    fis.close();
    st.close();
    conn.close();
  }
  private static Connection getConnection() throws Exception {
    Class.forName("org.hsqldb.jdbcDriver");
    String url = "jdbc:hsqldb:mem:data/tutorial";

    return DriverManager.getConnection(url, "sa", "");
  }
}




















Home »
  Java Tutorial »
    java.sql »




DatabaseMetaData
ParameterMetaData
PreparedStatement
ResultSet
Timestamp