From: Satish Talim Date: 2007-05-04T19:17:13+09:00 Subject: Re: How to connect from JRuby to a MySql database? ------=_Part_40719_13801747.1178273832048 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline On 5/4/07, Ronny wrote: > > Could someone kindly sketch an example (or point me to a related > website), how > to query a MySql database from JRuby? > > I guess there are several possibilities available (via JDBC, since > JRuby is based on > Java, and maybe using some Ruby library too, since JRuby is intended > to be compatible > to Ruby), but I didn't find any documentation for it on the web. > > Ronald > > Here's an example of accessing a MS-Access database table using JRuby. You can make the relevant changes for mySQL. require 'java' module JavaLang include_package "java.lang" end module JavaSql include_package 'java.sql' end begin JavaLang::Class.forName("sun.jdbc.odbc.JdbcOdbcDriver") conn = JavaSql::DriverManager.getConnection("jdbc:odbc:DSN"); stmt = conn.createStatement rs = stmt.executeQuery("select studname from student") while (rs.next) do puts rs.getString("studname") end rs.close stmt.close conn.close() rescue JavaLang::ClassNotFoundException puts "ClassNotFoundException" rescue JavaSql::SQLException puts "SQLException" end Hope this helps. Satish Talim http://rubylearning.com/ ------=_Part_40719_13801747.1178273832048--