<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>OraQA &#187; Jdeveloper</title>
	<atom:link href="http://oraqa.com/category/jdeveloper/feed/" rel="self" type="application/rss+xml" />
	<link>http://oraqa.com</link>
	<description>Oracle Question and Answer</description>
	<lastBuildDate>Tue, 06 Jul 2010 00:08:52 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to pass ARRAYS of records from Java/Tomcat to Oracle</title>
		<link>http://oraqa.com/2008/03/18/how-to-pass-arrays-of-records-from-javatomcat-to-oracle/</link>
		<comments>http://oraqa.com/2008/03/18/how-to-pass-arrays-of-records-from-javatomcat-to-oracle/#comments</comments>
		<pubDate>Tue, 18 Mar 2008 19:56:24 +0000</pubDate>
		<dc:creator>ravivedala</dc:creator>
				<category><![CDATA[Jdeveloper]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://oraqa.com/2008/03/18/how-to-pass-arrays-of-records-from-javatomcat-to-oracle/</guid>
		<description><![CDATA[Environment : JDK 1.5, Tomcat 5.5, Oracle 10gR2.
Let&#8217;s suppose that you have a web app where you get some records from the user interface and from your DAO, you are trying to pass the records as Oracle ARRAYS to database. Here is a step by step example.
Yes, it&#8217;s a nightmare as you have to take [...]]]></description>
			<content:encoded><![CDATA[<p>Environment : JDK 1.5, Tomcat 5.5, Oracle 10gR2.</p>
<p>Let&#8217;s suppose that you have a web app where you get some records from the user interface and from your DAO, you are trying to pass the records as Oracle ARRAYS to database. Here is a step by step example.</p>
<p>Yes, it&#8217;s a nightmare as you have to take care of some steps. But, once you understand what to do and what are the issues, it&#8217;s pretty easy.</p>
<p>Use Case : Let us suppose that we have a java bean Employee and we are trying to send an array of employee records at a time to database.</p>
<p>/*Step 1 : Create a object type in the database*/<br />
/*<br />
CREATE OR REPLACE TYPE &#8220;EMP_TYPE&#8221; is object(<br />
emp_id       Varchar2(500),<br />
emp_name     varchar2(500));<br />
*/<br />
/*<br />
Step 2 : Create a type EMP_TYPE_TABLE</p>
<p>CREATE OR REPLACE TYPE &#8220;EMP_TYPE_TAB&#8221;;<br />
*/</p>
<p>/*Step 3 : Create a Java bean which maps the attributes of the above object type in Step 2.*/</p>
<p>import java.io.Serializable;<br />
import java.sql.SQLData;<br />
import java.sql.SQLException;<br />
import java.sql.SQLInput;<br />
import java.sql.SQLOutput;</p>
<p>public class Employee implements SQLData, Serializable{<br />
static final long serialVersionUID = 4070409649129120458L;<br />
public Employee(){}</p>
<p>// constructor that takes parameters<br />
// getters and setters for emp_id, emp_name<br />
// You have to implement readSQL() and writeSQL() methods, as shown below.<br />
// This is where you are mapping the Employee table&#8217;s columns to the Employee<br />
//java bean.</p>
<p>public void readSQL(SQLInput stream, String typeName) throws SQLException {<br />
this.emp_id = stream.readString();<br />
this.emp_name = stream.readString();<br />
}<br />
public void writeSQL(SQLOutput stream) throws SQLException {<br />
stream.writeString(emp_id);<br />
stream.writeString(emp_name);<br />
}<br />
}</p>
<p>//EmpDAO class gets a connection to the database and passes the data.</p>
<p>public class EmpDAO{<br />
java.sql.Connection conn;<br />
java.sql.Connection dconn;<br />
/*<br />
Step 1 : Get database connection<br />
This is a very important step. To pass your records of data as Arrays, you need to get a oracle.jdbc.driver.T4CConnection and then use ArrayDescriptor&#8217;s. So, how do you get a T4CConnection ?</p>
<p>To get T4CConnection from java.sql.Connection, you need to cast like this :<br />
t4cConn = ((DelegatingConnection)conn).getInnermostDelegate();</p>
<p>If you are working on tomcat, you have two options to get a DataSource in your context.xml.<br />
a) By using apache commons-dbcp<br />
OR<br />
b) by directly using javax.sql.DataSource.<br />
Let&#8217;s see how to get the T4CConnection in both these cases.<br />
*/</p>
<p>public void sendRecordsToDB(){</p>
<p>//Use Case (a) : if you configured apache commons-dbcp<br />
BasicDataSource ds = (BasicDataSource)ctx.lookup(jndiName);<br />
ds.setAccessToUnderlyingConnectionAllowed(true);<br />
conn = ds.getConnection();<br />
dconn = ((DelegatingConnection)conn).getInnermostDelegate();</p>
<p>//Use Case (b) : if you are directly using javax.sql.DataSource</p>
<p>BasicDataSource bds = new BasicDataSource();<br />
bds.setDriverClassName(&#8221;");<br />
bds.setUsername(&#8221;");<br />
bds.setPassword(&#8221;<br />
&#8220;);<br />
bds.setUrl(&#8221;jdbc:oracle:thin:@&#8221;);<br />
bds.setAccessToUnderlyingConnectionAllowed(true);<br />
conn = bds.getConnection();<br />
dconn = ((DelegatingConnection)conn).getInnermostDelegate();</p>
<p>/*So, using either of the above approaches we got dconn, which is an instance of T4CConnection.*/</p>
<p>/* Now let&#8217;s build an array list of employees.<br />
*/<br />
final List listOfEmployees = new LinkedList();</p>
<p>Employee e1 = new Employee();<br />
e.setEmpId(1);<br />
e.setEmpName(&#8221;Ravi&#8221;);</p>
<p>listOfEmployees.add(e1);</p>
<p>Employee e2 = new Employee();<br />
e.setEmpId(2);<br />
e.setEmpName(&#8221;Vedala&#8221;);</p>
<p>listOfEmployees.add(e2);</p>
<p>// Now, create an array descriptor</p>
<p>ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor( &#8220;EMP_TYPE_TAB&#8221;, dconn );</p>
<p>ARRAY array_to_pass = new ARRAY( descriptor, dconn, (Object[]) listOfEmployees.toArray());<br />
ps = (OracleCallableStatement)dconn.prepareCall(&#8221;begin insert_employees(:1); end;&#8221;);<br />
ps.setARRAY( 1, array_to_pass );<br />
ps.execute();<br />
conn.commit();<br />
}</p>
<p>/*<br />
- See how simple and beautiful is the procedure.<br />
- Using the TABLE() function, you can treat the whole array as a table as EMP_TYPE_TAB is a nested table.<br />
*/</p>
<p>PROCEDURE insert_employees(p_emparray in EMP_TYPE_TAB) AS<br />
BEGIN<br />
/* INSERT ARRAY OF RECORDS IN TO THE EMP TABLE*/<br />
INSERT INTO scd_company_staging<br />
(emp_id,emp_name)<br />
SELECT * FROM TABLE(p_comparray);<br />
END insert_employees;</p>
<p>The nightmare exception for Java/Oracle developers :-)</p>
<p>java.lang.ClassCastException: oracle.jdbc.driver.T4CConnection cannot be cast to oracle.jdbc.OracleConnection<br />
	at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:149)<br />
at oracle.sql.ArrayDescriptor.createDescriptor(ArrayDescriptor.java:115)<br />
&#8230;&#8230;&#8230;..</p>
<p>Solution :<br />
a)You will see the above exception, if you have ojdbc14.jar in your war file. You would be having ojdbc14.jar on your classpath for compiling your java classes. Use it only for compilation. Don&#8217;t include it in the build to Tomcat. ie., the war file of your web app should NOT have ojdbc14.jar in it.</p>
<p>b) Make sure that the Oracle thin driver (eg : ojdbc14.jar) is in tomcat&#8217;s common\lib.<br />
<font color="green"><br />
Long live &#8220;Tom Kyte&#8221;.</font></p>
<p>Good Luck !!<br />
r-a-v-i</p>
]]></content:encoded>
			<wfw:commentRss>http://oraqa.com/2008/03/18/how-to-pass-arrays-of-records-from-javatomcat-to-oracle/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
