[英]Groovy cannot connect to postgresql database?
我正在使用 Ready API 1.4.0,並且我已經嘗試使用這個 groovy 代碼連接到 postgresql。
import groovy.sql.Sql
import java.sql.Driver
def driver = Class.forName('org.postgresql.Driver').newInstance() as Driver
def props = new Properties()
props.setProperty("DB_user", "user")
props.setProperty("DB_password", "user")
def conn = driver.connect("jdbc:postgresql://localhost:54320/database_name", props)
def sql = new Sql(conn)
try {
sql.eachRow("select * from user") {
log.debug(it)
}
} finally {
sql.close()
conn.close()
}
然后我收到了這個錯誤:
java.lang.ClassNotFoundException:org.postgresql.Driver at line:4
我在 bin/ext postgresql-9.4-1205.jdbc42.jar 中添加了這個 jar 庫
請問有什么幫助嗎? 謝謝你。
嘗試使用 Grape 獲取您的 PostgreSQL 驅動程序。
@GrabConfig(systemClassLoader=true)
@Grab(group='org.postgresql', module='postgresql', version='9.4-1205-jdbc42')
請參閱http://docs.groovy-lang.org/latest/html/documentation/grape.html#Grape-JDBCDrivers
我遇到過同樣的問題。 所以手動下載postgres驅動,加載到Jenkins主服務器java的lib目錄下。
PostgreSQL JDBC 4.2 驅動程序,42.2.14 https://jdbc.postgresql.org/download.html
重啟 Jenkins 服務后,它的工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.