This question already has answers here:
How do I store a string longer than 4000 characters in an Oracle Database using Java/JDBC?
(4 answers)
Closed 4 years ago.
I am facing problems to store CLOB data through JDBC to Oracle 9i database. I am using PreparedStatement and setting the data as setString(index, value). The value is a large XML string. The driver I am using is ojdbc14.jar. Still I am getting an error saying " java.lang.Exception: Data size bigger than max size for this type: 4723".
My XML is not so big that it will not fit in CLOB field. Still getting this error. Please help.
I searched various sites it it did not solve the problem, the setClob() method is not supported by ojdbc14 driver.
The XML probably isn't too big for a CLOB (4GB!) but is for using setString (4000 characters). The textbook solution would probably be to use the setCharacterStream method instead:
PreparedStatement ps = ...;
String xml = "<xml>somve verly long data... </xml>";
ps.setCharacterStream(1, new StringReader(xml), xml.length());
Related
I have an Oracle procedure with an input Clob and returns an output Clob.
When i'm trying to recover the value, i reach the object, if i try to read the toString fro the object, i take the "oracle.sql.CLOB#625a8a83" . But when i want to read the object, in anyways i tryed, allways get a connection closed exception.
in my code:
MapSqlParameterSource parametros = new MapSqlParameterSource();
// setting input parameter
parametros.addValue("PE_IN", new SqlLobValue("IN DATA CLOB", new DefaultLobHandler()),
Types.CLOB);
// Executing call
Map<String, Object> out = jdbcCall.execute(parametros);
salida.setDatosRespuesta(out.get("PS_OUT").toString());
if i change the last line for this:
Clob clob = (Clob) out.get("PS_OUT");
long len = clob.length();
String rtnXml = clob.getSubString(1, (int) len);
i get the connection close error. I tryed in several ways and i can't solve this problem. Any ideas?
I think yo are using the SimpleJdbcCall of the spring framework. If so the database configuration are the default configurations for the oracle driver, you need to increase the time out for the reading of the values for the connection. Check the DatabaseMetaData documentation, also check the OracleConnection properies CONNECTION_PROPERTY_THIN_READ_TIMEOUT_DEFAULT. This happends because you are reading a large data from the database remember that de CLOB can have until 4gb of data
You need to keep in mind that is this process is very common in your application you need to consider the quantity of the connections to the database in order to have always enable connections to your database to guarantee your application availability
Regarding the out.get("PS_OUT").toString() this basically only show the hash that represents your object that the reason beacause why that line works fine
This question already has answers here:
Insert CLOB into Oracle database
(6 answers)
Oracle Sql Developer "string literal too long" error
(5 answers)
How do I store a string longer than 4000 characters in an Oracle Database using Java/JDBC?
(4 answers)
Closed 4 years ago.
when I'm trying to insert blob in database having more than 4000 characters I'm getting fallowing error can anyone help on this.is there any way other than procedures to insert data :
Error report -
SQL Error: ORA-01704: string literal too long
01704. 00000 - "string literal too long"
*Cause: The string literal is longer than 4000 characters.
*Action: Use a string literal of at most 4000 characters.
Longer values may only be entered using bind variables
Instead of setString use setCharacterStream (for instance).
Of course assuming you are already using a PreparedStatement:
try (PreparedStatement stmt = conn.prepareStatement(someSql)) {
String large = String.format("%-4001s", "xxx");
StringReader reader = new StringReader(large);
stmt.setCharacterStream(1, reader, large.length());
stmt.setLong(2, someId);
int updateCount = stmt.executeUpdate();
}
I need to store base64 encoded document in Sybase database via stored procedure. I use JTDS driver.
As long as I am not trying to store my PDF document everything is fine. However as soon I attempt to path encoded string. I get the following error
java.sql.SQLException: Sybase does not support char parameters > 255
bytes.
Here is the code
conn.setAutoCommit(false);
cs = conn.prepareCall("{call sp_save_pdf (?,?,?,?)}");
cs.setString(++i, id);
cs.setString(++i, source);
cs.setString(++i, base64EncodedDocument);
cs.registerOutParameter(++i, java.sql.Types.INTEGER);
cs.execute();
When Sybase Developers execute this procedure but they use JConnect driver. Here is my question does JTDS driver not support usage of long strings and I need to use JConnect or do I miss something in my code?
Thanks
Issue was with JTDS driver as soon as I switched to JConnect driver everything began to work as expected.
I am inserting clob data into mysql database...here is my code
Clob cl=dbCon.createClob();
cl.setString(1,userAbout);
dbCon.setAutoCommit(false);
PreparedStatement insertClob=dbCon.prepareStatement("UPDATE user_data SET user_about=? WHERE user_id=?");
insertClob.setClob(1,cl);
insertClob.setInt(2,userId);
int count= insertClob.executeUpdate();
if(count==1){dbCon.commit();dbCon.close();out.write("success");}
else{dbCon.rollback();dbCon.close();out.print("error");}
this is throwing an exception
java.lang.AbstractMethodError: org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.createClob()Ljava/sql/Clob;
whats the problem here? and How can I solve it?
You don't need createClob() anyway. I find using setCharacterStream() to be much more stable (and much better supported by all JDBC drivers).
StringReader reader = new StringReader(userAbout);
PreparedStatement insertClob = dbCon.prepareStatement("UPDATE user_data SET user_about=? WHERE user_id=?");
insertClob.setCharacterStream(1, reader, userAbout.length());
insertClob.setInt(2,userId);
int count= insertClob.executeUpdate();
This also works with an INSERT statement. No need to create any intermediate clob (or blob) objects.
Note that I changed the wrong index 8 to the correct index 2 to match the placeholders in the UPDATE statement.
Many modern drivers also handle a "simple" setString() just as well for CLOB columns. It's worth trying out - would reduce the code even more.
I try to create a hibernate mapping for an oracle database. The datebase is pretty old from before oracle 8 but is now on 10. Hibernate reverse engineering balks at a long raw column. This datatype is deprecated and should be converted to blob.
But this is not my database. If the customer refuses to convert how would a hibernate mapping look like ?
Try mapping it to byte[].
If you get java.sql.SQLException: Stream has already been closed, then try setting useFetchSizeWithLongColumn = true in the connection properties for the OJDBC driver. See the OracleDriver API