Sunday, July 29, 2012

Inserting BLOB data through JDBC



How to Insert Blob Data into database using JDBC ..
String blobData="My Blob Data String.";
BigDecimal id= BigDecimal.ONE;
String QUERY = "INSERT INTO USER_BLOB(ID, BLOB_DATA) VALUES(?,?) ";
        Connection connection = null;
        PreparedStatement statement = null;
        try{
            connection =  Connections.getDataBaseConnection();
            statement =connection.prepareStatement(QUERY);
            byte[]  blobData =  blobData.getBytes();
            InputStream inputStream = new ByteArrayInputStream(blobData);
            statement.setBinaryStream(1,inputStream, blobData.length);
            statement.setBigDecimal(2,id);
            statement.executeUpdate();
            connection.commit();
        }catch(Exception e){

        }finally {
            statement.close();
            connection.close();
        }