Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 831
Templog Log Avatar asked Nov 20 '15 19:11

Templog Log


2 Answers

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

like image 195
Emanuel Seidinger Avatar answered Oct 04 '22 15:10

Emanuel Seidinger


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.

like image 39
Ahamed N Avatar answered Oct 04 '22 15:10

Ahamed N