Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-12-2008, 10:13 AM
Member
 
Join Date: Aug 2008
Posts: 6
Thilkumar82 is on a distinguished road
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("jdbcracleci:@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
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-12-2008, 06:07 PM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 18
baskar.nitt is on a distinguished road
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)

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; } }
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-13-2008, 10:30 AM
Member
 
Join Date: Aug 2008
Posts: 6
Thilkumar82 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-13-2008, 11:04 AM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 18
baskar.nitt is on a distinguished road
whats your java version?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 08-13-2008, 11:23 AM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 18
baskar.nitt is on a distinguished road
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..

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; } }
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 08-13-2008, 11:24 AM
Member
 
Join Date: Aug 2008
Posts: 6
Thilkumar82 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 08-13-2008, 12:10 PM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 18
baskar.nitt is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 08-13-2008, 12:25 PM
Member
 
Join Date: Jul 2008
Posts: 34
jack239 is on a distinguished road
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-
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 08-13-2008, 01:24 PM
Member
 
Join Date: Aug 2008
Posts: 6
Thilkumar82 is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 08-13-2008, 01:33 PM
baskar.nitt's Avatar
Member
 
Join Date: Apr 2008
Location: Chennai, India
Posts: 18
baskar.nitt is on a distinguished road
yes.... jre 1.5 installation is the exact reason..
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do i export data from an oracle database to a CSV file BharathL Database 3 08-05-2008 05:19 AM
[SOLVED] how to save hindi text into Oracle Database sanjeevtarar Database 9 07-23-2008 03:01 PM
Probeleme with insert into database oracle bachtoutou New To Java 0 05-24-2008 01:56 PM
Using JDBC to connect to ORACLE database Java Tip Java Tips 0 02-10-2008 01:27 PM
Using PreparedStatement to insert image into database Java Tip Java Tips 0 02-10-2008 01:25 PM


All times are GMT +3. The time now is 09:08 AM.


VBulletin, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org