Results 1 to 10 of 10
  1. #1
    Thilkumar82 is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default 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

  2. #2
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    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;
    	}
    
    }

  3. #3
    Thilkumar82 is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default

    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

  4. #4
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    whats your java version?

  5. #5
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    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;
    		}
    
    	}

  6. #6
    Thilkumar82 is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default

    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

  7. #7
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

    Default

    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.

  8. #8
    jack239 is offline Member
    Join Date
    Jul 2008
    Posts
    35
    Rep Power
    0

    Default

    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

  9. #9
    Thilkumar82 is offline Member
    Join Date
    Aug 2008
    Posts
    6
    Rep Power
    0

    Default

    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

  10. #10
    baskar.nitt's Avatar
    baskar.nitt is offline Member
    Join Date
    Apr 2008
    Location
    Chennai, India
    Posts
    19
    Rep Power
    0

Similar Threads

  1. Replies: 14
    Last Post: 12-07-2010, 11:41 AM
  2. Replies: 3
    Last Post: 08-05-2008, 04:19 AM
  3. Probeleme with insert into database oracle
    By bachtoutou in forum New To Java
    Replies: 0
    Last Post: 05-24-2008, 12:56 PM
  4. Using JDBC to connect to ORACLE database
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-10-2008, 12:27 PM
  5. Replies: 0
    Last Post: 02-10-2008, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •