English 中文(简体)
How to initialize the SQLite3 JDBC driver in JRuby?
原标题:

How do you access SQLite3 via JDBC without using active record?

问题回答

Here s an example that works with JRuby 1.6.6 (in Ruby 1.8 compat mode) with jdbc-sqlite3 3.7.2.

require  rubygems 
require  jdbc/sqlite3 
require  java 

org.sqlite.JDBC                 # load the driver so DriverManager detects it 
#Java::OrgSqlite::JDBC          # alternate means of same

connection = java.sql.DriverManager.getConnection  jdbc:sqlite:test.sqlite3 
begin
  statement = connection.createStatement
  begin
    statement.executeUpdate("create table user (name varchar, pass varchar)")
    statement.executeUpdate("insert into user values ( alice , 1234)")
    statement.executeUpdate("insert into user values ( bob , 5678)")
    statement.executeUpdate("insert into user values ( charlie ,  asdf )")

    rs = statement.executeQuery("select * from user")
    begin
      puts "user	pass"
      while rs.next
        puts ["#{rs.getString(1)}",
              "#{rs.getString(2)}"].join("	")
      end
    ensure
      rs.close
    end

  ensure
    statement.close
  end
ensure
  connection.close
end

Output:

$ rm -f test.sqlite3; ruby sql.rb
user    pass
------------
alice   1234
bob     5678
charlie asdf

Install the jdbc-sqlite3 gem

Then, in your jruby script:

require  jdbc/sqlite3 
url = "jdbc:sqlite:path.to.your.db"
begin        
    Java::org.sqlite.JDBC #initialize the driver
    connection = JavaLang::DriverManager.getConnection(url) #grab your connection
rescue => error      
    #handle error
end

The jdbc driver file sqlite-3.5.8.jar gets copied into the gem directory. It needs to be copied into the jruby/lib directory. Application also needs restart.

The driver file can be downloaded directly from here

http://files.zentus.com/sqlitejdbc/sqlitejdbc-v056.jar

http://www.zentus.com/sqlitejdbc/




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签