Skip to content

Instantly share code, notes, and snippets.

@rsinger
Created May 10, 2011 20:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsinger/965289 to your computer and use it in GitHub Desktop.
Save rsinger/965289 to your computer and use it in GitHub Desktop.
class DbConnection
require "jruby"
attr_reader :conn
require 'jdbc/sybase-jconnect-5.5.jar'
@@connection = nil
def initialize
java.lang.Thread.currentThread.setContextClassLoader(JRuby.runtime.jruby_class_loader)
import java.sql.Statement
import java.sql.Connection
import java.sql.SQLException
import java.sql.Types
import java.sql.DriverManager
DriverManager.registerDriver Java::com.sybase.jdbc2.jdbc.SybDriver.new
@conn = DriverManager.getConnection("#{CONFIG['database']['url']}?user=#{CONFIG['database']['username']}&password=#{CONFIG['database']['password']}")
end
def query(sql, limit=CONFIG["global_options"]["maximum_results"])
stmt = @conn.createStatement(java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE,java.sql.ResultSet.CONCUR_READ_ONLY)
stmt.setFetchSize(limit)
rs = stmt.executeQuery(sql)
return rs
end
def self.create
unless @@connection
@@connection = self.new
end
@@connection
end
def close
@conn.close
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment