Share & Learn

Friday, June 22, 2007

The Code snippet for MySQL using JRuby

require 'java'

module JavaLang
include_package "java.lang"
end

module JavaSql
include_package 'java.sql'
end

begin
JavaLang::Class.forName("com.mysql.jdbc.Driver").newInstance
conn = JavaSql::DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/?user=");
stmt = conn.createStatement
rs = stmt.executeQuery("select name from user")
while (rs.next) do
puts rs.getString("name")
end
rs.close
stmt.close
conn.close()
rescue JavaLang::ClassNotFoundException
puts "ClassNotFoundException"
rescue JavaSql::SQLException
puts "SQLException"
end

blog comments powered by Disqus
blog comments powered by Disqus