Results 1 to 10 of 10
- 08-12-2008, 08:13 AM #1
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
How to insert java Object in oracle database
Hi,
I want to insert java Object into oracle database..
I am using BLOB column and code as follows ..
Data1 data1=new Data1();
data1.a=500;
data1.b=600;
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con = DriverManager.getConnection("jdbc:oracle:oci:@mydb ", "username", "pwd");
PreparedStatement pst = con.prepareStatement("insert into obj_temp(user_id,data_object) values (?,?)");
pst.setInt(1,10);
pst.setObject(2,data1);
when executing this .. i am getting "Invalid column type" SQLException..
I am doing this for later use of this object from database..
here i want to get A's value 500 and B's value 600 when i retrieving the object from datadase..
Thnks in Advance,
Thilkumar
- 08-12-2008, 04:07 PM #2
Hi ,
To store java objects in oracle db you have to convert the object to byte array. Check the following sample...
It will help you.. (insert objects to oracle db and retrieve objects from oracle db)
Java Code:package com.samples; import java.io.*; import java.sql.*; import oracle.jdbc.*; public class Check1 { public static void main(String [] args) throws Exception { BufferedReader reader=null; try { reader = new BufferedReader(new InputStreamReader(System.in)); System.out.println("1. Insert rows"); System.out.println("2. Select rows"); System.out.println("Enter your option : "); int option = Integer.parseInt(reader.readLine()); switch(option) { case 1: insertRows(); break; case 2: selectRows(); break; default: System.out.println("Pls enter your option between 1 to 2"); } } catch(Exception e) { e.printStackTrace(); } } public static void insertRows() { Connection connection =null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); connection = DriverManager.getConnection("jdbc:oracle:thin:@servername:1521:orcl", "username", "password"); Data1 data1=new Data1(300,500); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream objOstream = new ObjectOutputStream(baos); objOstream.writeObject(data1); byte[] bArray = baos.toByteArray(); System.out.println("*** bArray = " + bArray); PreparedStatement objStatement = connection.prepareStatement("insert into sample(user_id,data_object) values (?,?)"); objStatement.setInt(1,10); objStatement.setBytes(2, bArray); objStatement.execute(); } catch(SQLException sqlEx) { sqlEx.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { connection=null; } } public static void selectRows() { Statement stmt = null; ResultSet rs = null; String sQuery =""; Connection connection =null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); connection = DriverManager.getConnection("jdbc:oracle:thin:@servername:1521:orcl", "username", "password"); sQuery = "select * from sample"; byte [] newArray = null; stmt = connection.createStatement(); stmt.executeQuery(sQuery); rs = stmt.getResultSet(); while(rs.next()) { newArray = rs.getBytes("data_object"); System.out.println("newArray = " + newArray); ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(newArray)); Data1 objData1 = (Data1) ois.readObject(); ois.close(); if(objData1 == null) { System.out.println("Data1 Object is null"); } else { System.out.println("a = " + objData1.geta() + " b = " + objData1.getb()); } } rs.close(); } catch(SQLException sqlEx) { sqlEx.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { connection=null; } } } Data1.java: ============ package com.samples; import java.io.*; public class Data1 implements Serializable { private int a; private int b; public Data1() { } public Data1(int a, int b) { this.a=a; this.b=b; } public int geta() { return this.a; } public void seta(int a) { this.a=a; } public int getb() { return this.b; } public void setb() { this.b=b; } }
- 08-13-2008, 08:30 AM #3
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
Thank you so much basker,
It works for inserting into db.. but gives following error for selecting
"
java.io.StreamCorruptedException: invalid stream header
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at Check1.selectRows(Check1.java:110)
at Check1.main(Check1.java:29)
"
Regards,
Thilkumar
- 08-13-2008, 09:04 AM #4
whats your java version?
- 08-13-2008, 09:23 AM #5
Replace the insertRows() method with the following code and check it.
The code which i have posted first time is works fine in jdk 1.5... so pls check with jdk 1.5..
Java Code:public static void insertRows() { Connection connection =null; try { Class.forName("oracle.jdbc.driver.OracleDriver"); connection = DriverManager.getConnection("jdbc:oracle:thin:@ramcobl104:1521:orcl", "wfuser", "wfuser"); Data1 data1=new Data1(300,500); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream objOstream = new ObjectOutputStream(baos); objOstream.writeObject(data1); objOstream.flush(); objOstream.close(); byte[] bArray = baos.toByteArray(); System.out.println("*** bArray = " + bArray); PreparedStatement objStatement = connection.prepareStatement("insert into sample(user_id,data_object) values (?,?)"); objStatement.setInt(1,10); objStatement.setBytes(2, bArray); objStatement.execute(); } catch(SQLException sqlEx) { sqlEx.printStackTrace(); } catch(Exception e) { e.printStackTrace(); } finally { connection=null; } }
- 08-13-2008, 09:24 AM #6
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
Hi baskar,
I got solution for select from db using the foll,
InputStream is = rs.getBlob("data_object").getBinaryStream();
ObjectInputStream oip = new ObjectInputStream(is);
Object object = oip.readObject();
It is working correctly..
Thanks a lot..
One more doubt .. My java version is j2sdk1.4.2_03
but when get from DOS promt command (java -version)
it gives
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode)
why?
Regards,
Thilkumar
- 08-13-2008, 10:10 AM #7
The path is set to jdk 1.5 in your comp.
Go to My Computer icon -> right click --> properties -->Advanced Tab --> Click Environment Variables--> Path
Check the value of Path variable. It may set to JDK 1.5.
- 08-13-2008, 10:25 AM #8
Member
- Join Date
- Jul 2008
- Posts
- 35
- Rep Power
- 0
Yep I think the environmental PATH variable is referring to the 1.5 version.
New to Java/PHP/Javascript development?
For free help go to- www.techcubetalk.com
- 08-13-2008, 11:24 AM #9
Member
- Join Date
- Aug 2008
- Posts
- 6
- Rep Power
- 0
Everything is refering to jdk1.4 but .. in my system we installed jre 1.5...
i think this is the reason for that..
Thanks..
thilkumar
- 08-13-2008, 11:33 AM #10
Similar Threads
-
[SOLVED] how to save hindi text into Oracle Database
By sanjeevtarar in forum JDBCReplies: 14Last Post: 12-07-2010, 10:41 AM -
How do i export data from an oracle database to a CSV file
By BharathL in forum JDBCReplies: 3Last Post: 08-05-2008, 03:19 AM -
Probeleme with insert into database oracle
By bachtoutou in forum New To JavaReplies: 0Last Post: 05-24-2008, 11:56 AM -
Using JDBC to connect to ORACLE database
By Java Tip in forum Java TipReplies: 0Last Post: 02-10-2008, 11:27 AM -
Using PreparedStatement to insert image into database
By Java Tip in forum Java TipReplies: 0Last Post: 02-10-2008, 11:25 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks