Templog Log
Templog Log

Reputation: 415

Groovy cannot connect to postgresql database?

I'm using Ready API 1.4.0 and I have tried this groovy code to connect to 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()
}

Then I received this error:

java.lang.ClassNotFoundException:org.postgresql.Driver at line:4

I added this jar lib and in bin/ext postgresql-9.4-1205.jdbc42.jar

Any help, please? Thank you.

Upvotes: 5

Views: 8635

Answers (2)

Ahamed N
Ahamed N

Reputation: 95

I had the same issue. So manually download the postgres driver and loaded into Jenkins master server java's lib directory.

PostgreSQL JDBC 4.2 Driver, 42.2.14 https://jdbc.postgresql.org/download.html

After restarting Jenkins service, its working.

Upvotes: 1

Emanuel Seidinger
Emanuel Seidinger

Reputation: 1272

Try use Grape to get your PostgreSQL driver.

@GrabConfig(systemClassLoader=true)
@Grab(group='org.postgresql', module='postgresql', version='9.4-1205-jdbc42')

See http://docs.groovy-lang.org/latest/html/documentation/grape.html#Grape-JDBCDrivers

Upvotes: 7

Related Questions