<?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>Mission Data Blog &#187; Systems Integration</title>
	<atom:link href="http://www.missiondata.com/blog/tag/systems-integration/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.missiondata.com/blog</link>
	<description>Louisville-based Web Development &#38; Software Engineering</description>
	<lastBuildDate>Tue, 24 Jan 2012 14:58:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>accessing as400 databases with Ruby, Java, and the RubyJavaBridge</title>
		<link>http://www.missiondata.com/blog/system-administration/46/accessing-as400-databases-with-ruby-java-and-the-rubyjavabridge/</link>
		<comments>http://www.missiondata.com/blog/system-administration/46/accessing-as400-databases-with-ruby-java-and-the-rubyjavabridge/#comments</comments>
		<pubDate>Tue, 25 Apr 2006 04:13:27 +0000</pubDate>
		<dc:creator>darrend</dc:creator>
				<category><![CDATA[System Administration]]></category>
		<category><![CDATA[as400]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[Systems Integration]]></category>

		<guid isPermaLink="false">http://blogs.missiondata.com/?p=46</guid>
		<description><![CDATA[iSeries systems and Ruby are separate universes right now; I know of no native Ruby way to connect to an AS400 system. However, with the help of Java we can bridge the gap. Probably the simplest example is connecting to an iSeries system using JDBC to select from some tables, and with the right libraries [...]]]></description>
			<content:encoded><![CDATA[<p>iSeries systems and Ruby are separate universes right now; I know of no native Ruby way to connect to an AS400 system. However, with the help of Java we can bridge the gap.</p>
<p>Probably the simplest example is connecting to an iSeries system using JDBC to select from some tables, and with the right libraries it is straightforward. It&#8217;s also fun to be doing some nifty quick Ruby but with some preexisting and proven Java libraries.</p>
<p>Also, if you use Java and Ruby, definitely put arton&#8217;s <a href="http://arton.no-ip.info/collabo/backyard/?RubyJavaBridge">RubyJavaBridge</a> in your pocket; you&#8217;ll end up using it more than once.</p>
<p>What you&#8217;ll need:</p>
<ul>
<li>Ruby (well yea)
<li>Java (ok)
<li>access to an AS400 system (that&#8217;s the whole point here&#8230;)<br />For testing, I&#8217;ve been using a free account available from <a href="http://as400.holgerscherer.de/indexeng.html">Holger Scherer Software und Beratung</a>.
<li><tt>jt400.jar</tt> jdbc drivers from <a href="http://jt400.sourceforge.net/">JTOpen</a>, a great open-source Java library for AS400 access.
<li><a href="http://arton.no-ip.info/collabo/backyard/?RubyJavaBridge">RubyJavaBridge</a> is the keystone for this.
</ul>
<p>Installing the software is the hardest part of this exercise, but it&#8217;s more tedium than anything. Once you&#8217;ve got it all downloaded and running the fun can begin.</p>
<p><span id="more-46"></span></p>
<pre>
<code>require 'rjb'
Rjb::load('jtopen_5_0/lib/jt400.jar',['-Djdbc.drivers=com.ibm.as400.access.AS400JDBCDriver'])

DriverManager = Rjb::import('java.sql.DriverManager')

begin
  connection = DriverManager.getConnection('jdbc:as400://as400.holgerscherer.de','YOUR_USERNAME','YOUR_PASSWORD')
  statement = connection.prepareStatement("SELECT count(*) FROM SYSIBM.TABLES")
  result_set = statement.executeQuery

  while(result_set.next())
    puts result_set.getObject(1).toString
  end
ensure
  result_set.close if defined?(result_set) &amp;&amp; !statement.nil?
  statement.close if defined?(statement) &amp;&amp; !statement.nil?
  connection.close if defined?(connection) &amp;&amp; !connection.nil?
end</code>
</pre>
<pre>
<code class="console">$ ruby as400_rjb.rb
19138</code>
</pre>
<p>Very readable, I think. Java and Ruby actually play well together given half the chance and <a href="http://arton.no-ip.info/collabo/backyard/?RubyJavaBridge">arton&#8217;s fantastic bridge</a>.</p>
<p>Compare this with the pure Java implementation.</p>
<pre>
<code>import java.sql.*;

public class As400Jdbc
{
  public static void main(String args[])
  {
    Connection connection = null;
    try
    {
      connection = DriverManager.getConnection("jdbc:as400://as400.holgerscherer.de","USER","PASS");
      PreparedStatement statement = connection.prepareStatement("SELECT COUNT(*) FROM SYSIBM.TABLES");

      ResultSet resultSet = statement.executeQuery();

      while(resultSet.next())
      {
        System.out.println(resultSet.getObject(1));
      }
    }
    catch(SQLException e)
    {
      throw new RuntimeException(e);
    }
    finally
    {
      try{ if(connection!=null) connection.close(); }
      catch(SQLException e)
      {
        throw new RuntimeException(e);
      }
    }

  }
}</code>
</pre>
<pre>
<code class="console">$ javac As400Jdbc.java
$ java -cp jtopen_5_0/lib/jt400.jar:. -Djdbc.drivers=com.ibm.as400.access.AS400JDBCDriver As400Jdbc
19138</code>
</pre>
<p>The Ruby code and the Java code are nearly one-for-one so far, and that&#8217;s fine. Still, let&#8217;s see if we can&#8217;t pull the Java code more into Ruby&#8217;s world. The <code>begin...end</code> stuff gets tiresome; it would be nice if these Java classes could handle the resource allocation and disposal drudgery for us, using blocks.</p>
<pre>
<code>require 'rjb'
Rjb::load('/home/darren/dload/jtopen_5_0/lib/jt400.jar',['-Djdbc.drivers=com.ibm.as400.access.AS400JDBCDriver'])

DriverManager = Rjb::import('java.sql.DriverManager')
class &lt;&lt;DriverManager
  def with_connection(*args)
    begin
      connection = DriverManager.getConnection(*args)
      yield connection
    ensure
      connection.close
    end
  end
end

DriverManager.with_connection('jdbc:as400://as400.holgerscherer.de','USER_NAME','USER_PASS') do |conn|
  statement = conn.prepareStatement("SELECT count(*) FROM SYSIBM.TABLES")
  result_set = statement.executeQuery()
  result_set.next()
  puts result_set.getObject(1).toString
end</code>
</pre>
<p>I know I&#8217;m a geek because stuff like this makes me giggle. So many worlds are colliding here&#8211;Ruby, Java, AS400, DB2, the client system (Linux in this case)&#8211;and it all comes together so nicely. </p>
<p>I&#8217;m not saying I&#8217;d build an app out of this, but it will more than suffice for ad-hoc queries and quick scripts.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.missiondata.com/blog/system-administration/46/accessing-as400-databases-with-ruby-java-and-the-rubyjavabridge/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
	</channel>
</rss>

