我在grails项目中使用了H2内存数据库。我的应用程序与H2数据库一起正常运行。 我想使用groovy连接到H2数据库,以从数据库中获取数据。
import groovy.sql.Sql
import java.sql.Driver
class psqlh2 {
static void main(String[] args) {
def driver = Class.forName('org.h2.Driver').newInstance() as Driver
def props = new Properties()
props.setProperty("user", "sa")
props.setProperty("password", "")
def conn = driver.connect("jdbc:h2:mem:~/databaseName;DB_CLOSE_DELAY=-1",props)
def sql = new Sql(conn)
def query = "SELECT * FROM company"
try {
sql.eachRow(query) { row ->
println(row)
}
} finally {
sql.close()
conn.close()
}
}
警告:未能执行:SELECT * FROM company,因为:找不到表“ COMPANY”; SQL语句: SELECT * FROM公司[42102-199] 线程“主” org.h2.jdbc.JdbcSQLSyntaxErrorException中的异常:未找到表“ COMPANY”;
请帮帮我。
答案 0 :(得分:0)
将内存替换为文件
def driver = Class.forName('org.h2.Driver').newInstance() as Driver
def props = new Properties()
props.setProperty("user", username)
props.setProperty("password", password)
return driver.connect("jdbc:h2:file:${absolutePath};DB_CLOSE_DELAY=-1;IFEXISTS=true", props)