<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>CUBRID JDBC Tutorial - Part 2</title>
        <link>http://www.cubrid.org/?mid=accessing_cubrid_with_jdbc_part2</link>
        <description>CUBRID JDBC Tutorial - Part 2</description>
        <language>en</language>
        <pubDate>Mon, 18 Feb 2013 05:27:10 -0800</pubDate>
        <lastBuildDate>Mon, 18 Feb 2013 06:34:07 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>Accessing CUBRID ...</title>
            <dc:creator>CUBRID</dc:creator>
            <link>http://www.cubrid.org/accessing_cubrid_with_jdbc_part2</link>
            <guid isPermaLink="true">http://www.cubrid.org/accessing_cubrid_with_jdbc_part2</guid>
                                    <description><![CDATA[<p class="MsoNormal"><b><span style="font-size:24.0pt;line-height:106%;color:#44546A">Accessing CUBRID with
JDBC - Part II<o:p></o:p></span></b></p>

<p class="MsoNormal"><span style="color:#44546A">February 2013<o:p></o:p></span></p>

<p class="MsoNoSpacing">This is the 2<sup>nd</sup> part of the latest CUBRID JDBC
tutorial we have published recently. In this part, we will go through some more
advanced topics, to help you start quickly with CUBRID in a Java development
environment.</p>

<p class="MsoNoSpacing">The software prerequisites are the same we mentioned in
the <a href="/?mid=accessing_cubrid_with_jdbc_part1" target="_self">1<sup>st</sup>
part</a>, so if all is set and done, we are ready to move on!</p>

<p class="MsoNoSpacing"><br /></p>

<p class="MsoNoSpacing"><span style="font-size:20.0pt">Database schema<a name="_GoBack"></a><o:p></o:p></span></p>

<p class="MsoNoSpacing"><br /></p>

<p class="MsoNoSpacing">JDBC comes up with everything you need to query and
change a database schema. Remember – CUBRID Manager Client is using JDBC to
provide all those powerful tools to manage a CUBRID database!</p>

<p class="MsoNoSpacing">Have you wondered, for example, how does the CUBRID
Manager get a table SQL schema definition…?</p>

<p class="MsoNoSpacing" style="text-align: center;"><o:p>&nbsp;</o:p><img src="http://www.cubrid.org/files/attach/images/236657/274/588/schema.png" alt="schema.png" width="533" height="414" editor_component="image_link" style="text-align: center;"/><span style="text-align: center;"></span></p>

<p class="MsoNoSpacing">Well, there is no “magic” in JDBC which simply gives a
SQL table definition back to you in one call – it’s just Java code which uses
the <i>Metadata</i> API standard.</p>

<p class="MsoNoSpacing">So, we challenge you, as an exercise, to write the JAVA
JDBC code that does create a table DLL SQL definition! <span style="font-family: Wingdings;">J</span></p>

<p class="MsoNoSpacing">In the example below, for a start, we will show you how
to create the DDL SQL for an index (for simplicity we will consider that there
is just one index defined on the table – let’s use the <i>event&nbsp;</i><b>demodb</b> table):</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">public&nbsp;static&nbsp;String&nbsp;getIndexDDL(String&nbsp;tableName)&nbsp;throws&nbsp;SQLException,&nbsp;ClassNotFoundException&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;indexRetrieve&nbsp;=&nbsp;"SELECT&nbsp;*&nbsp;FROM&nbsp;db_index"&nbsp;+&nbsp;"WHERE&nbsp;class_name&nbsp;=&nbsp;'"&nbsp;+&nbsp;tableName&nbsp;+&nbsp;"'";<br />
&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;SQL&nbsp;=&nbsp;"CREATE&nbsp;";<br />
&nbsp;&nbsp;&nbsp;&nbsp;Statement&nbsp;stmt&nbsp;=&nbsp;conn.createStatement();<br />
&nbsp;&nbsp;&nbsp;&nbsp;ResultSet&nbsp;rs&nbsp;=&nbsp;stmt.executeQuery(indexRetrieve);<br />
&nbsp;&nbsp;&nbsp;&nbsp;rs.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;SQL&nbsp;+=&nbsp;(rs.getString("is_unique").equals("YES")&nbsp;==&nbsp;true&nbsp;?&nbsp;"UNIQUE&nbsp;"&nbsp;:&nbsp;"")&nbsp;+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(rs.getString("is_reverse").equals("YES")&nbsp;==&nbsp;true&nbsp;?&nbsp;"REVERSE&nbsp;"&nbsp;:&nbsp;"")&nbsp;+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"INDEX&nbsp;&#92;""&nbsp;+&nbsp;rs.getString("index_name")&nbsp;+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"&#92;"&nbsp;ON&nbsp;&#92;""&nbsp;+&nbsp;rs.getString("class_name")&nbsp;+&nbsp;"&#92;"(&#92;n";<br />
&nbsp;&nbsp;&nbsp;&nbsp;Statement&nbsp;stmt2&nbsp;=&nbsp;conn.createStatement();<br />
&nbsp;&nbsp;&nbsp;&nbsp;ResultSet&nbsp;rs2&nbsp;=&nbsp;stmt2.executeQuery("SELECT&nbsp;*&nbsp;FROM&nbsp;db_index_key&nbsp;"&nbsp;+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"WHERE&nbsp;class_name&nbsp;=&nbsp;'"&nbsp;+&nbsp;tableName&nbsp;+&nbsp;"'&nbsp;AND&nbsp;"&nbsp;+<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"index_name&nbsp;=&nbsp;'"&nbsp;+&nbsp;rs.getString("index_name")&nbsp;+&nbsp;"'");<br />
&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(rs2.next())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SQL&nbsp;+=&nbsp;"&#92;""&nbsp;+&nbsp;rs2.getString("key_attr_name")&nbsp;+&nbsp;"&#92;"&nbsp;"&nbsp;+&nbsp;rs2.getString("asc_desc");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(rs2.next())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;SQL&nbsp;+=&nbsp;",&#92;n&#92;""&nbsp;+&nbsp;rs2.getString("key_attr_name")&nbsp;+&nbsp;"&#92;"&nbsp;"&nbsp;+&nbsp;rs2.getString("asc_desc");<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;SQL&nbsp;+=&nbsp;"&#92;n);";<br />
&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt2.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;rs2.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;rs.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;conn.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br />
&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;SQL;<br />
}</div>

<p class="MsoNoSpacing">The output is:</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Sql" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">CREATE&nbsp;UNIQUE&nbsp;INDEX&nbsp;"pk_event_code"&nbsp;ON&nbsp;"event"(<br />
"code"&nbsp;ASC<br />
);</div>

<p class="MsoNoSpacing"><o:p>&nbsp;</o:p></p>

<p class="MsoNoSpacing"><span style="font-size:20.0pt">Error handling<o:p></o:p></span></p>

<p class="MsoNoSpacing"><o:p>&nbsp;</o:p></p>

<p class="MsoNoSpacing">Proper error handling is a key element in developing a
reliable application. Many things can go wrong when your application deals with
a database - it can be your code causing an error or it can be the database
fault (for example - the database is not up &amp; running, or there are no
correct credentials etc.).</p>

<p class="MsoNoSpacing">For example, let’s consider a code example from the <a href="/?mid=accessing_cubrid_with_jdbc_part1" target="_self">1<sup>st</sup>
part of the tutorial</a>, with a minor change:&nbsp;</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">//&nbsp;Create&nbsp;test&nbsp;table<br />
stmt.execute("drop&nbsp;table&nbsp;if&nbsp;exists&nbsp;test_metadata");<br />
stmt.execute("create&nbsp;table&nbsp;test_metadata(id&nbsp;int,&nbsp;seq&nbsp;set(int))");<br />
<br />
//&nbsp;Query&nbsp;table&nbsp;metadata<br />
ResultSet&nbsp;rs&nbsp;=&nbsp;stmt.executeQuery("select&nbsp;*&nbsp;from&nbsp;test_metadata");<br />
System.out.println("Element&nbsp;type:"&nbsp;+&nbsp;((CUBRIDResultSetMetaData)&nbsp;rs.getMetaData()).getElementType(1));<br />
System.out.println("Element&nbsp;type&nbsp;name:&nbsp;"&nbsp;+&nbsp;((CUBRIDResultSetMetaData)&nbsp;rs.getMetaData()).getElementTypeName(1));</div>

<p class="MsoNoSpacing">When you run this code, the following error is displayed:</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">Exception&nbsp;in&nbsp;thread&nbsp;"main"&nbsp;cubrid.jdbc.driver.CUBRIDException:&nbsp;The&nbsp;type&nbsp;of&nbsp;the&nbsp;column&nbsp;should&nbsp;be&nbsp;a&nbsp;collection&nbsp;type.<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;cubrid.jdbc.driver.CUBRIDResultSetMetaData.getElementType(CUBRIDResultSetMetaData.java:658)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;atError.main(Error.java:20)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.reflect.NativeMethodAccessorImpl.invoke0(Native&nbsp;Method)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;atjava.lang.reflect.Method.invoke(Method.java:601)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;at&nbsp;com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)</div>

<p class="MsoNoSpacing">So what do you do…?</p>

<p class="MsoNoSpacing">In this particular case, the error is on the application
side – the wrong column index was used (“<i>1</i>”
instead of “<i>2</i>”). So the right thing
to do is fix the code and re-run. </p>

<p class="MsoNoSpacing">As an exception, if you are not familiar with CUBRID or
the error message needs more clarification, what you can do is to look directly
at the error messages in the JDBC driver (remember, it’s all open source!). You
will find the CUBRID specific error messages and codes in the file <i>CUBRIDJDBCErrorCode.java</i>.</p>

<p class="MsoNoSpacing">On the other hand, you should consider using <i>try-catch</i> for unexpected errors. Here is
an error handling code example you could use in your own application:</p>

<p class="MsoNoSpacing"><u>Some code to simulate an unrecoverable error:</u></p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">try&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;ResultSet rs&nbsp;=&nbsp;stmt.executeQuery("select&nbsp;*&nbsp;from&nbsp;non_existing_table,&nbsp;and&nbsp;get&nbsp;an&nbsp;error!");<br />
&nbsp;&nbsp;&nbsp;&nbsp;rs.next();<br />
&nbsp;&nbsp;&nbsp;&nbsp;rs.close();<br />
}&nbsp;catch&nbsp;(SQLException&nbsp;ex)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Throwable&nbsp;e&nbsp;:&nbsp;ex)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(e&nbsp;instanceofSQLException)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace(System.err);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println("SQLState:&nbsp;"&nbsp;+&nbsp;((SQLException)&nbsp;e).getSQLState());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println("Error&nbsp;Code:&nbsp;"&nbsp;+&nbsp;((SQLException)&nbsp;e).getErrorCode());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println("Message:&nbsp;"&nbsp;+&nbsp;e.getMessage());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Throwable&nbsp;t&nbsp;=&nbsp;ex.getCause();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(t&nbsp;!=&nbsp;null)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Cause:&nbsp;"&nbsp;+&nbsp;t);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;t&nbsp;=&nbsp;t.getCause();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</div>

<p class="MsoNoSpacing"><u>The output result is:<o:p></o:p></u></p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">SQLState:&nbsp;null<br />
Error&nbsp;Code:&nbsp;-493<br />
Message:&nbsp;jdbc:cubrid:localhost:30000:demodb:public:********:<br />
Syntax:&nbsp;In&nbsp;line&nbsp;1,&nbsp;column&nbsp;35&nbsp;before&nbsp;'&nbsp;get&nbsp;an&nbsp;error!'<br />
Syntax&nbsp;error:&nbsp;unexpected&nbsp;'and'</div>&nbsp;<p></p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="623" valign="top" style="width:467.5pt;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FBE4D5;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing">As you can see, usually the error messages provided in
  the CUBRID JDBC driver can be very helpful to the end user to track exactly
  the root problem, all you have to do is to implement a proper error handling
  logic in your application.</p>
  
 </tr>
</tbody></table>

<p class="MsoNoSpacing">You can find more useful information about error handling
and JDBC <a href="http://docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html">here</a>.</p>

<p class="MsoNoSpacing">&nbsp;</p>

<p class="MsoNoSpacing"><span style="font-size:20.0pt">Using transactions in JDBC<o:p></o:p></span></p>

<p class="MsoNoSpacing"><br /></p>

<p class="MsoNoSpacing">In this section we will take a look at using database
transactions with JDBC. </p>

<p class="MsoNoSpacing">In particular we will show:</p>

<p class="MsoNoSpacing" style="margin-left:.5in;text-indent:-.25in;mso-list:l0 level1 lfo1"><!--[if !supportLists]-->-<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><!--[endif]-->How to get information about the current
database status: isolation level, auto-commit mode </p>

<p class="MsoNoSpacing" style="margin-left:.5in;text-indent:-.25in;mso-list:l0 level1 lfo1"><!--[if !supportLists]-->-<span style="font-size: 7pt; line-height: normal; font-family: 'Times New Roman';">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span><!--[endif]-->An example of nested transactions with error
handling</p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="623" valign="top" style="width:467.5pt;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FBE4D5;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing">Some more information about transactions can be found
  here:</p>
  <p class="MsoNoSpacing"><a href="http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html">http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html</a></p>
  
 </tr>
</tbody></table>

<p class="MsoNoSpacing"><o:p>&nbsp;</o:p><u>Getting information about the current database status:</u></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">//&nbsp;Get&nbsp;the&nbsp;database&nbsp;Transaction&nbsp;Isolation&nbsp;Level<br />
System.out.print("Isolation&nbsp;level:&nbsp;");<br />
switch(connection.getTransactionIsolation())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;1:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("READ&nbsp;COMMITTED&nbsp;CLASS&nbsp;with&nbsp;READ&nbsp;UNCOMMITTED&nbsp;INSTANCES");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;2:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("READ&nbsp;COMMITTED&nbsp;CLASS&nbsp;with&nbsp;READ&nbsp;COMMITTED&nbsp;INSTANCES");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;3:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("REPEATABLE&nbsp;READ&nbsp;CLASS&nbsp;with&nbsp;READ&nbsp;UNCOMMITTED&nbsp;INSTANCES");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;4:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("REPEATABLE&nbsp;READ&nbsp;CLASS&nbsp;with&nbsp;READ&nbsp;COMMITTED&nbsp;INSTANCES&nbsp;(or&nbsp;CURSOR&nbsp;STABILITY)");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;5:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("REPEATABLE&nbsp;READ&nbsp;CLASS&nbsp;with&nbsp;REPEATABLE&nbsp;READ&nbsp;INSTANCES");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />
&nbsp;&nbsp;&nbsp;&nbsp;case&nbsp;6:<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("SERIALIZABLE");<br />
}<br />
//&nbsp;Get&nbsp;the&nbsp;database&nbsp;AutoCommit&nbsp;Mode<br />
System.out.println("AutoCommit&nbsp;mode:&nbsp;"&nbsp;+&nbsp;connection.getAutoCommit());</div>

<p></p>

<p class="MsoNoSpacing"><u>The output result is:</u></p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">Isolation&nbsp;level:&nbsp;READ&nbsp;COMMITTED&nbsp;CLASS&nbsp;with&nbsp;READ&nbsp;UNCOMMITTED&nbsp;INSTANCES<br />
AutoCommit&nbsp;mode:&nbsp;true</div>&nbsp;<p></p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="638" valign="top" style="width:6.65in;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FBE4D5;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing"><u>Some more information about CUBRID Isolation Level
  can be found here:<o:p></o:p></u></p>
  <p class="MsoNoSpacing"><a href="/manual/90/en/Transaction%20Isolation%20Level">http://www.cubrid.org/manual/90/en/Transaction%20Isolation%20Level</a><u><o:p></o:p></u></p>
  
 </tr>
</tbody></table>

<p class="MsoNoSpacing"><u><br /></u></p>

<p class="MsoNoSpacing"><u>Nested transactions with error handling:</u></p>

<p class="MsoNoSpacing">We will create a table in an outer transaction and in the
inner transaction we will create a view based on the table. If the view
creation fails (we will force it to fail for the demo purpose), the table
creation will be rollback.</p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="638" valign="top" style="width:6.65in;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FBE4D5;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing">As you probably know, you can’t really embed
  transactions in CUBRID.</p>
  <p class="MsoNoSpacing">To implement nested transactions, you need to use <a href="/manual/90/en/Savepoint%20and%20Partial%20Rollback">SAVEPOINTS</a>.</p>
  
 </tr>
</tbody></table>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">Statement&nbsp;stmt&nbsp;=&nbsp;connection.createStatement();<br />
try{<br />
&nbsp;&nbsp;&nbsp;&nbsp;connection.setAutoCommit(false);<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt.execute("SAVEPOINT&nbsp;SP1");<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt.execute("create&nbsp;table&nbsp;test_tran(id&nbsp;int)");<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt.execute("SAVEPOINT&nbsp;SP2");<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt.execute("create&nbsp;view&nbsp;test_view&nbsp;as&nbsp;select&nbsp;nothing&nbsp;from&nbsp;test_tran");<br />
&nbsp;&nbsp;&nbsp;&nbsp;connection.commit();<br />
}&nbsp;catch(SQLException&nbsp;ex)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;System.out.println("Rolling&nbsp;back&nbsp;the&nbsp;transaction!");<br />
&nbsp;&nbsp;&nbsp;&nbsp;stmt.execute("ROLLBACK&nbsp;WORK&nbsp;TO&nbsp;SP1");<br />
}</div>&nbsp;<p></p>

<p class="MsoNoSpacing"><span style="font-size:20.0pt">CUBRID Java Stored
procedures<o:p></o:p></span></p>

<p class="MsoNoSpacing"><br /></p>

<p class="MsoNoSpacing">If you are familiar with CUBRID, you know that one of the
most powerful CUBRID features is the support for <a href="/manual/90/en/Java%20Stored%20Function%7CProcedure-Overview">Java
stored procedures</a>. There is almost no limit to what you can achieve when
you combine stored procedures with the CUBRID engine!</p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="623" valign="top" style="width:467.5pt;border:solid windowtext 1.0pt;
  mso-border-alt:solid windowtext .5pt;background:#FBE4D5;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing">We have already published an introduction about Java
  stored procedures – please take a look <a href="/cubrid_java_stored_procedures">here</a>.</p>
  <p class="MsoNoSpacing">Another interesting example can be found on this <a href="http://cubrid-talks.blogspot.com/2012/11/emulate-long-running-SQL-queries-in-CUBRID.html">blog</a>.</p>

</tr></tbody></table>

<p class="MsoNoSpacing">Here, we will demo an example of using stored procedures
and JDBC, to implement a “missing” SQL feature – <b>create a copy of a VIEW</b>. You supply an existing VIEW name, the
desired duplicate VIEW name and the procedure will create the duplicated VIEW.</p>

<p class="MsoNoSpacing">Looking from the technical perspective, the code will
look at the VIEW definition in the system objects and will use it to create the
new VIEW:</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Java" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">import&nbsp;java.sql.*;<br />
import&nbsp;cubrid.jdbc.driver.*;<br />
<br />
public&nbsp;class&nbsp;StoredProc&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;static&nbsp;void&nbsp;duplicateView(String&nbsp;view_name,&nbsp;String&nbsp;new_name)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;try&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName("cubrid.jdbc.driver.CUBRIDDriver");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Connection&nbsp;connection&nbsp;=&nbsp;DriverManager.getConnection("jdbc:default:connection:");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PreparedStatement&nbsp;pstmt&nbsp;=&nbsp;connection.prepareStatement("select&nbsp;vclass_def&nbsp;from&nbsp;db_vclass&nbsp;where&nbsp;vclass_name&nbsp;=&nbsp;?");<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pstmt.setString(1,&nbsp;view_name);<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ResultSet&nbsp;rs&nbsp;=&nbsp;pstmt.executeQuery();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if(rs.next())&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Statement&nbsp;stmt&nbsp;=&nbsp;connection.createStatement();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stmt.execute("CREATE&nbsp;VIEW&nbsp;"&nbsp;+&nbsp;new_name&nbsp;+&nbsp;"&nbsp;AS&nbsp;"&nbsp;+&nbsp;rs.getString(1));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stmt.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pstmt.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;connection.close();<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;catch&nbsp;(Exception&nbsp;e)&nbsp;{<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(e.getMessage());<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}<br />
&nbsp;&nbsp;&nbsp;&nbsp;}<br />
}</div>

<p class="MsoNoSpacing">Next we have to compile the java class and load it to the
<b>demodb</b> database using the <b>loadjava&nbsp;</b>utility<b>.<o:p></o:p></b></p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Bash" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">javac&nbsp;-cp&nbsp;"C:&#92;CUBRID&#92;jdbc&#92;cubrid_jdbc.jar"&nbsp;StoredProc.java<br />
C:&#92;CUBRID&#92;bin&#92;loadjava.exe&nbsp;-y&nbsp;demodb&nbsp;StoredProc.class</div>

<p>After loading the java class to the database, we will use
CUBRID Manager to create the stored procedure and call it. So login to the
demodb database, right-click on the <b>Stored
procedure&nbsp;</b>node and select <b>Create
Procedure. </b>Fill in the requested fields:</p>

<p class="MsoNoSpacing" align="center" style="text-align:center"><img src="http://www.cubrid.org/files/attach/images/236657/274/588/create_procedure.png" alt="create_procedure.png" width="578" height="465" editor_component="image_link"/>
<br /><!--[endif]--></p>

<p class="MsoNoSpacing" align="center" style="text-align: left;">To test the procedure let’s create a view to duplicate.
In the Query Editor run the following SQL:</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Sql" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">CREATE&nbsp;VIEW&nbsp;test_view&nbsp;AS&nbsp;SELECT&nbsp;*&nbsp;FROM&nbsp;code</div>

<p class="MsoNoSpacing">Now we are ready to use the procedure we just created by
running the following SQL script in the Query Editor.</p>

<p class="MsoNoSpacing"></p>

<div editor_component="code_highlighter" code_type="Sql" title="" first_line="1" collapse="false" highlight="" nogutter="false" style="border:#666 1px dotted;border-left:#2AE 5px solid;padding:5px;background:#FAFAFA url('./modules/editor/components/code_highlighter/code.png') no-repeat top right;">CALL&nbsp;duplicate_view('test_view',&nbsp;'copy_of_test_view')</div>

<p class="MsoNoSpacing">As you can see, the copy of <b>test_view</b> was created and we can display the results:</p>

<p class="MsoNoSpacing" align="center" style="text-align:center"><img src="http://www.cubrid.org/files/attach/images/236657/274/588/call_procedure.png" alt="call_procedure.png" width="564" height="538" editor_component="image_link"/>&nbsp;</p>

<p class="MsoNoSpacing" align="center" style="text-align: left;"><span style="font-size: 20pt;">More references and links</span></p>

<p class="MsoNoSpacing">We already introduced you in the first part of the
tutorial to some useful references and links. Here, we will give some more, to
help you find online other great code samples and documentation.</p>

<table class="MsoNormalTable" border="1" cellspacing="0" cellpadding="0" style="border: none;">
 <tbody><tr>
  <td width="200" valign="top" style="width: 149.75pt; border-top-style: solid; border-bottom-style: solid; border-left-style: solid; border-color: windowtext; border-width: 1pt; padding: 0in 5.4pt;">
  <p class="MsoNoSpacing">Java CUBRID Stored procedures</p>
  </td>
  <td width="424" valign="top" style="width:317.75pt;border:solid windowtext 1.0pt;
  border-left:none;mso-border-left-alt:solid windowtext .5pt;mso-border-alt:
  solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing"><a href="/cubrid_java_stored_procedures">http://www.cubrid.org/cubrid_java_stored_procedures</a></p>
  
 </tr>
 <tr>
  <td width="200" valign="top" style="width: 149.75pt; border-bottom-style: solid; border-left-style: solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-top-style: none; padding: 0in 5.4pt;">
  <p class="MsoNoSpacing">CUBRID Hibernate</p>
  </td>
  <td width="424" valign="top" style="width:317.75pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing"><a href="/cubrid_hibernate_tutorial">http://www.cubrid.org/cubrid_hibernate_tutorial</a></p>
  
 </tr>
 <tr>
  <td width="200" valign="top" style="width: 149.75pt; border-bottom-style: solid; border-left-style: solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-top-style: none; padding: 0in 5.4pt;">
  <p class="MsoNoSpacing">JDBC sample program</p>
  </td>
  <td width="424" valign="top" style="width:317.75pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing"><a href="/manual/90/en/JDBC%20Sample%20Program">http://www.cubrid.org/manual/90/en/JDBC%20Sample%20Program</a></p>
  
 </tr>
 <tr>
  <td width="200" valign="top" style="width: 149.75pt; border-bottom-style: solid; border-left-style: solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-top-style: none; padding: 0in 5.4pt;">
  <p class="MsoNoSpacing">SourceForge CUBRID JDBC examples</p>
  </td>
  <td width="424" valign="top" style="width:317.75pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing"><a href="http://sourceforge.net/projects/cubridsrccode/?source=directory">http://sourceforge.net/projects/cubridsrccode/?source=directory</a></p>
  
 </tr>
 <tr>
  <td width="200" valign="top" style="width: 149.75pt; border-bottom-style: solid; border-left-style: solid; border-right-color: windowtext; border-bottom-color: windowtext; border-left-color: windowtext; border-right-width: 1pt; border-bottom-width: 1pt; border-left-width: 1pt; border-top-style: none; padding: 0in 5.4pt;">
  <p class="MsoNoSpacing">JDBC tutorial</p>
  </td>
  <td width="424" valign="top" style="width:317.75pt;border-top:none;border-left:
  none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;
  mso-border-top-alt:solid windowtext .5pt;mso-border-left-alt:solid windowtext .5pt;
  mso-border-alt:solid windowtext .5pt;padding:0in 5.4pt 0in 5.4pt">
  <p class="MsoNoSpacing"><a href="http://www.tutorialspoint.com/jdbc/index.htm">http://www.tutorialspoint.com/jdbc/index.htm</a></p>
  
 </tr>
</tbody></table>

<p class="MsoNoSpacing">If you would like to get the code we used in this
tutorial, as well as the IntelliJ project – please contact us at: <a href="/contact">http://www.cubrid.org/contact</a>.</p>

<p class="MsoNoSpacing">We hope you enjoyed this new CUBRID JDBC tutorial!</p>

<p class="MsoNoSpacing">As always, we kindly ask you to let us know your feedback
and suggestions, so we can improve!</p>

<p class="MsoNoSpacing">And remember to stay tuned for more <a href="/wiki_tutorials">tutorials</a> to come…</p>

<p class="MsoNoSpacing"><o:p>&nbsp;</o:p></p>

<p class="MsoNoSpacing"><b>Thank you!<o:p></o:p></b></p>

<p class="MsoNoSpacing">The CUBRID API team</p>]]></description>
                        <pubDate>Mon, 18 Feb 2013 05:29:06 -0800</pubDate>
                        <category>jdbc</category>
                        <category>tutorial</category>
                        <category>cubrid</category>
                                </item>
            </channel>
</rss>
