registerOutParameter - setBinaryStream - Problems inserting Blob - setRAW
439362Apr 22 2005 — edited Apr 25 2005As posted in metalink (was: "Problems inserting BLOB/InputStream with ojdbc14.jar for 10g - Data size bigger than max size for this type"):
Using setBinaryStream for large Blobs works as long as I don't register outParameters.
Query that works: "INSERT INTO blobtest (attachment_id,name,data) VALUES(blobtest_SEQ.nextval,?,?)";
Query that fails = "BEGIN INSERT INTO blobtest (attachment_id,name,data) VALUES( blobtest_SEQ.nextval,?,?) RETURN attachment_id INTO ? ; END;"
------------------
The necessary tables were created by hand:
CREATE TABLE blobtest ( NAME CHAR(255), data BLOB, attachment_id NUMBER(38))
And
CREATE SEQUENCE TBL_ATTACHMENT_SEQ
The output was: <<user: SEE
pw: QD
instantiating oracle driver
query: INSERT INTO blobtest (attachment_id,name,data) VALUES(TBL_ATTACHMENT_SEQ.nextval,?,?)
uploaded no Return Parameter blob of size: 256809
query: BEGIN INSERT INTO blobtest (attachment_id,name,data) VALUES(TBL_ATTACHMENT_SEQ.nextval,?,?) RETURN attachment_id INTO ? ; END;
java.sql.SQLException: Datengr÷▀e gr÷▀er als max. Gr÷▀e f³r diesen Typ: 256809
at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:125)
at
oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java
:162)
at
oracle.jdbc.driver.OraclePreparedStatement.setRAW(OraclePreparedState
ment.java:5342)
at
oracle.jdbc.driver.OraclePreparedStatement.setBinaryStreamInternal(Or
aclePreparedStatement.java:6885)
at
oracle.jdbc.driver.OracleCallableStatement.setBinaryStream(OracleCall
ableStatement.java:4489)
at BlobTest.writeBlob(BlobTest.java:161)
at BlobTest.testBlob(BlobTest.java:118)
at BlobTest.main(BlobTest.java:92)
error: Datengr÷▀e gr÷▀er als max. Gr÷▀e f³r diesen Typ:
256809>>
here the java test case:
/*
* Created on 25.08.2004 $Id: BlobTest.java,v 1.4 2005/04/22 11:21:11 hauser Exp $
* as posted in metalink jdbc forum 050405 and responses by
* ansriniv@in.oracle.com
*/
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Types;
public class BlobTest {
private static String FILE_NAME = "c:/temp/veryLargeFile.pdf";
public BlobTest() {
//
}
final static int ORACLE = 1;
final static int MYSQL = 2;
private String jdbcUrl = "jdbc:mysql://localhost/test?user=monty&password=greatsqldb";
private int dbType = ORACLE;
private Driver driver = null;
private String user = "";
private String pw = "";
public static String SCHEME = "";
public static void main(String[] args) {
BlobTest bt = new BlobTest();
if (args[0] != null) {
System.out.println("dbType: " + args[0]);
if (args[0].toLowerCase().indexOf("oracle") != -1) {
bt.dbType = ORACLE;
}
if (args[0].toLowerCase().indexOf("mysql") != -1) {
bt.dbType = MYSQL;
}
} else {
System.out.println("not yet supported db type: " + args[0]);
System.exit(99);
}
if (args[1] != null) {
System.out.println("jdbcUrl: " + args[1]);
if (args[1].trim().length() != 0) {
bt.jdbcUrl = args[1].trim();
}
} else {
System.out.println("not yet supported jdbcUrl : " + args[1]);
System.exit(99);
}
if (args.length > 2 && args[2] != null) {
System.out.println("user: " + args[2]);
if (args[2].trim().length() != 0) {
bt.user = args[2].trim();
} else {
System.out.println("invalid user: " + args[2]);
System.exit(99);
}
}
if (args.length > 3 && args[3] != null) {
System.out.println("pw: " + args[3].substring(0, 2));
if (args[3].trim().length() != 0) {
bt.pw = args[3].trim();
} else {
System.out.println("invalid filename: " + args[3]);
System.exit(99);
}
}
if (args.length > 4 && args[4] != null) {
System.out.println("filename: " + args[4]);
if (args[4].trim().length() != 0) {
FILE_NAME = args[4].trim();
} else {
System.out.println("invalid filename: " + args[4]);
System.exit(99);
}
}
bt.setUp();
bt.testBlob();
}
public void setUp() {
try {
if (this.dbType == ORACLE) {
System.out.println("instantiating oracle driver ");
this.driver = (Driver) Class.forName(
"oracle.jdbc.driver.OracleDriver").newInstance();
} else {
this.driver = (Driver) Class.forName("com.mysql.jdbc.Driver")
.newInstance();
}
if (this.driver == null) {
System.out.println("oracle driver is null");
System.exit(88);
}
DriverManager.registerDriver(this.driver);
} catch (Exception e) {
e.printStackTrace();
System.out.println("error: " + e.getMessage());
}
}
public void testBlob() {
try {
this.writeBlob();
} catch (Exception e) {
e.printStackTrace();
System.out.println("error: " + e.getMessage());
}
}
/**
* testfunction
*/
private void writeBlob() throws Exception {
Connection conn = null;
PreparedStatement pStmt = null;
CallableStatement cStmt, cStmt2 = null;
InputStream in = null;
try {
File file = new File(BlobTest.FILE_NAME);
in = new FileInputStream(file);
conn = DriverManager.getConnection("jdbc:" + this.jdbcUrl,
this.user, this.pw);
conn.setAutoCommit(false);
String queryWorks = "INSERT INTO " + SCHEME
+ "blobtest (attachment_id,name,data) VALUES(" + SCHEME
+ "TBL_ATTACHMENT_SEQ.nextval,?,?)";
cStmt = conn.prepareCall(queryWorks);
System.out.println("query: " + queryWorks);
cStmt.setString(1, file.getAbsolutePath());
in = new FileInputStream(file);
cStmt.setBinaryStream(2, in, (int) file.length());
cStmt.execute();
System.out.println("uploaded no Return Parameter blob of size: "
+ file.length());
conn.commit();
String queryFails = "BEGIN INSERT INTO " + SCHEME
+ "blobtest (attachment_id,name,data) VALUES(" + SCHEME
+ "TBL_ATTACHMENT_SEQ.nextval,?,?)"
+ " RETURN attachment_id INTO ? ; END;";
cStmt2 = conn.prepareCall(queryFails);
System.out.println("query: " + queryFails);
cStmt2.setString(1, file.getAbsolutePath());
in = new FileInputStream(file);
cStmt2.setBinaryStream(2, in, (int) file.length());
cStmt2.registerOutParameter(3, Types.INTEGER);
cStmt2.execute();
System.out.println("uploaded blob of size: " + file.length()
+ " - id: " + cStmt2.getInt(3));
conn.commit();
} catch (Exception e) {
e.printStackTrace();
System.out.println("error: " + e.getMessage() + "\nname: "
+ BlobTest.FILE_NAME);
if (conn != null) {
try {
conn.rollback();
} catch (Exception e1) {
//
}
}
throw e;
} finally {
if (in != null) {
try {
in.close();
} catch (Exception e) {
;
}
}
if (pStmt != null) {
try {
pStmt.close();
} catch (Exception e) {
;
}
}
if (conn != null) {
try {
conn.close();
} catch (Exception e) {
;
}
}
}
}
}
and the batch file I use to start:
@setlocal
@echo off
rem $Id: runBlobTest.bat,v 1.2 2005/04/21 15:06:22 hauser Exp $
set classpath=../WEB-INF/classes;../WEB-INF/lib/ojdbc14.jar;
echo JAVA_HOME: %JAVA_HOME%
set JAVA_HOME=C:\PROGRA~1\Java\j2re1.4.1_02\
echo classpath: %classpath%
set javaCmd=C:\PROGRA~1\Java\j2re1.4.1_02\bin\java
%javaCmd% -version
%javaCmd% BlobTest "oracle" "oracle:thin://@ORADB.yourdomain.COM:1521:t300" "username" "password" "C:\Temp\veryLargeFile.pdf"
endlocal