Home » Developer & Programmer » JDeveloper, Java & XML » using setObject to store java objects in Oracle
using setObject to store java objects in Oracle [message #240180] Thu, 24 May 2007 05:52
asangapradeep
Messages: 128
Registered: October 2005
Location: UK
Senior Member
Hi,

this is my table desc
SQL> desc blob_test; 
Name           Type 
--------------------
ID            NUMBER(3) 
NAME          VARCHAR2(10) 
JAVAOBJ       BLOB 



and this is there java class I'm trying out. import statements have been removed.

public class OracleBlob 
{ 
public static void main(String[] args) 
{ 
try 
{ 
OracleOCIConnectionPool pool = new OracleOCIConnectionPool(); 
pool.setUser("tbx"); 
pool.setPassword("tbx"); 
pool.setURL("jdbc:oracle:oci:@test26"); 

Properties p = new Properties(); 

p.put(OracleOCIConnectionPool.CONNPOOL_MAX_LIMIT,"1"); 
p.put(OracleOCIConnectionPool.CONNPOOL_MIN_LIMIT,"0"); 
p.put(OracleOCIConnectionPool.CONNPOOL_INCREMENT,"0"); 

pool.setPoolConfig(p); 

Connection con = pool.getConnection(); 

Obj obj = new Obj(); 
obj.setVal(141.344); 

PreparedStatement prms = con.prepareStatement("insert into blob_test values (?,?,?)"); 

prms.setString(1,"00"); 
prms.setObject(2,"name"); 
prms.setObject(3,(Object)obj,Types.JAVA_OBJECT); 

prms.executeUpdate(); 
prms.close(); 
con.close(); 
pool.close(); 

} 
catch (Exception e) 
{ 
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates. 
} 

} 

}


this is the class of the object i'm trying to save


public class Obj implements java.io.Serializable 
{ 

private double amount; 

public void setVal(double amount){ 

this.amount = amount; 
} 

public double getVal(){ 
return this.amount; 
} 
} 


this fails with following error

Quote:

java.sql.SQLException: Invalid column type
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:227)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectCritical(OraclePreparedStatement.java:7791)
at oracle.jdbc.driver.OraclePreparedStatement.setObjectInternal(OraclePreparedStatement.java:7468)
at oracle.jdbc.driver.OraclePreparedStatement.setObject(OraclePreparedStatement.java:7841)
at OracleBlob.main(OracleBlob.java:55)
at java.lang.reflect.Method.invoke(Ljava.lang.Object;[Ljava.lang.Object;I)Ljava.lang.Object;(Unknown Source)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:86)



how can I save an object using setObject method. I tried setBytes methods to save the object and worked fine. But i want to use setObject.

thank you for any help given.
Previous Topic: Running Stored Procedure Error
Next Topic: libclntsh.so.10.1 causing JVM to crash
Goto Forum:
  


Current Time: Sun Nov 24 22:18:40 CST 2024