SqLite&摇篮 [英] SqLite & Gradle
本文介绍了SqLite&摇篮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经在build.gradle中添加了SQLite:
I've added SQLite in build.gradle:
dependencies {
compile 'org.xerial:sqlite-jdbc:3.8.9.1'
}
buildscript {
repositories {
jcenter()
mavenCentral()
}
dependencies {
classpath 'org.xerial:sqlite-jdbc:3.8.9.1'
}
}
一旦我想用代码连接到SQLite DB:
Once I want to connect to SQLite DB in code:
groovy.sql.Sql.newInstance(dbLocation, "org.sqlite.JDBC")
它抱怨:
java.lang.ClassNotFoundException: org.sqlite.JDBC
原因是什么?我该如何解决?
What is the reason? How can I fix it?
推荐答案
问题是您需要将JDBC驱动程序放入根类加载器中,而不是仅仅位于通用类路径上.
The problem is that you need to get the JDBC drivers into the root classloader rather than just being on the generalised classpath.
您有一些选择.其中之一是使用您自己的configuration
,然后通过GroovyObject
操纵根类加载器:
You have a few options. One of them is to use your own configuration
, and then manipulate the root classloader via GroovyObject
:
import groovy.sql.Sql
configurations {
sqllite
}
repositories {
mavenCentral()
}
dependencies {
sqllite 'org.xerial:sqlite-jdbc:3.8.9.1'
}
URLClassLoader loader = GroovyObject.class.classLoader
configurations.sqllite.each { File file ->
loader.addURL(file.toURL())
}
Sql sql = Sql.newInstance('jdbc:sqlite:test.db', "org.sqlite.JDBC")
task checkSql << {
sql.execute 'CREATE TABLE TIM(name CHAR(50))'
sql.eachRow('SELECT * FROM sqlite_master') { row ->
logger.lifecycle row.toString()
}
}
然后运行gradle checkSql
应该会导致:
$ gradle checkSql
:checkSql
[type:table, name:TIM, tbl_name:TIM, rootpage:2, sql:CREATE TABLE TIM(name CHAR(50))]
BUILD SUCCESSFUL
Total time: 4.374 secs
至少在Gradle 2.9中如此
At least, it does in Gradle 2.9
这篇关于SqLite&摇篮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文