# How to connect from JRuby to a MySql database?

**URL:** <https://www.ruby-forum.com/t/how-to-connect-from-jruby-to-a-mysql-database/103326>\
**Category:** Ruby\
**Created:** [May 4, 2007, 10:50am UTC](https://www.ruby-forum.com/t/how-to-connect-from-jruby-to-a-mysql-database/103326 "2007-05-04T10:50:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@ronny](https://www.ruby-forum.com/u/ronny)\
**Post date:** [May 4, 2007, 10:50am UTC](https://www.ruby-forum.com/t/how-to-connect-from-jruby-to-a-mysql-database/103326/1 "2007-05-04T10:50:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ronny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@ronny](https://www.ruby-forum.com/u/ronny)\
**Post date:** [May 4, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/how-to-connect-from-jruby-to-a-mysql-database/103326/2 "2007-05-04T12:18:06Z")

</div>

On 5/4/07, Ronny [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 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 T.

> **[Learn How to Blog and Build Websites for Profit!](https://heyblogging.com/)**
>
> Ready to take your blogging skills to the next level? Learn how to build a website that generates income and sets you up for success.

---

<div class="post-metadata">

**Author:** ![ronny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/94ad74/32.png) [@ronny](https://www.ruby-forum.com/u/ronny)\
**Post date:** [May 5, 2007, 5:08am UTC](https://www.ruby-forum.com/t/how-to-connect-from-jruby-to-a-mysql-database/103326/3 "2007-05-05T05:08:13Z")

</div>

On 5/4/07, Ronny [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ronald

You can refer to my blog post here -

> **[Learn How to Blog and Build Websites for Profit!](https://heyblogging.com)**
>
> Ready to take your blogging skills to the next level? Learn how to build a website that generates income and sets you up for success.

Satish T.

> **[Learn How to Blog and Build Websites for Profit!](https://heyblogging.com/)**
>
> Ready to take your blogging skills to the next level? Learn how to build a website that generates income and sets you up for success.
