java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5433

I want to check the database record but hitting error about driver for postgres, I did below and could not figure out what is missing, please help to check.

  1. add the postgres credentials at project settings and test connection passed
    jdbc:postgresql://localhost:5433/postgres?currentSchema=SCHEMA
  2. add external libraries, postgresql-42.2.6
  3. Create custom keyword package groovy, to connect to DB and get record from table1
    package dsdatabase
    import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
    import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
    import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
    import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import com.kms.katalon.core.annotation.Keyword
import com.kms.katalon.core.checkpoint.Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling
import com.kms.katalon.core.testcase.TestCase
import com.kms.katalon.core.testdata.TestData
import com.kms.katalon.core.testobject.TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI

import internal.GlobalVariable
import java.sql.ResultSet
import org.postgresql.Driver
import java.sql.DriverManager
import java.sql.SQLException
import java.sql.Statement
import org.postgresql.jdbc.PgConnection
import org.postgresql.jdbc.PgResultSet
import org.postgresql.jdbc.PgStatement
import java.sql.*

public class postgresHandler {

Connection c = null;
Statement stmt = null;

@Keyword
public void connectToPostgres(){
	try {
		Class.forName("org.postgresql.Driver")
		String url = "jdbc:postgresql://localhost:5433/postgres?currentSchema=SCHEMA";
		Properties props = new Properties();
		props.setProperty("user","postgres");
		props.setProperty("password","test");
		c = DriverManager.getConnection(url, props);

	} catch (Exception e) {
		e.printStackTrace()
		System.err.println(e.getClass().getName()+": "+e.getMessage())
		System.exit(0)
	}
	System.out.println("Opened database successfully")
}
@Keyword
public void selectData(){

	try {
		Class.forName("org.postgresql.Driver");
		c = DriverManager
				.getConnection("jdbc:postgresql://localhost:5433","postgres", "1234");
		c.setAutoCommit(false);
		System.out.println("Opened database successfully");

		stmt = c.createStatement();
		ResultSet rs = stmt.executeQuery( "select * from table1;" );
		while ( rs.next() ) {
			String id = rs.getString("id");
			System.out.println( "ID = " + id );
			System.out.println();
		}
		rs.close();
		stmt.close();
		c.close();
	} catch ( Exception e ) {
		System.err.println( e.getClass().getName()+": "+ e.getMessage() );
		System.exit(0);
	}
	System.out.println("Operation done successfully");
}
  1. Create test case to call the keyword
    import static com.kms.katalon.core.checkpoint.CheckpointFactory.findCheckpoint
    import static com.kms.katalon.core.testcase.TestCaseFactory.findTestCase
    import static com.kms.katalon.core.testdata.TestDataFactory.findTestData
    import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject

import java.sql.ResultSetMetaData

import org.postgresql.jdbc.PgResultSet

import com.kms.katalon.core.checkpoint.Checkpoint as Checkpoint
import com.kms.katalon.core.cucumber.keyword.CucumberBuiltinKeywords as CucumberKW
import com.kms.katalon.core.mobile.keyword.MobileBuiltInKeywords as Mobile
import com.kms.katalon.core.model.FailureHandling as FailureHandling
import com.kms.katalon.core.testcase.TestCase as TestCase
import com.kms.katalon.core.testdata.TestData as TestData
import com.kms.katalon.core.testobject.TestObject as TestObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
import internal.GlobalVariable as GlobalVariable

CustomKeywords.ā€˜dsdatabase.postgresHandler.connectToPostgresā€™()
CustomKeywords.ā€˜dsdatabase.postgresHandler.selectDataā€™()

  1. Run the test case and console has error about driver issue:
    SLF4J: The requested version 1.7.16 by your slf4j binding is not compatible with [1.6]
    SLF4J: See SLF4J Error Codes for further details.
    2019-07-26 11:55:20.955 INFO c.k.katalon.core.main.TestCaseExecutor - --------------------
    2019-07-26 11:55:20.957 INFO c.k.katalon.core.main.TestCaseExecutor - START Test Cases/dbtest
    2019-07-26 11:55:21.381 DEBUG testcase.dbtest - 1: dsdatabase.postgresHandler.connectToPostgres()
    java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5433
    at java.sql.DriverManager.getConnection(DriverManager.java:689)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at java_sql_DriverManager$getConnection.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:141)
    at dsdatabase.postgresHandler.connectToPostgres(dsdb.groovy:40)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:93)
    at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:325)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1215)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1024)
    at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:812)
    at groovy.lang.DelegatingMetaClass.invokeMethod(DelegatingMetaClass.java:144)
    at dsdatabase.postgresHandler.invokeMethod(dsdb.groovy)
    at com.kms.katalon.core.main.CustomKeywordDelegatingMetaClass.invokeStaticMethod(CustomKeywordDelegatingMetaClass.java:50)
    at org.codehaus.groovy.runtime.callsite.StaticMetaClassSite.call(StaticMetaClassSite.java:53)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:117)
    at Script1564112285605.run(Script1564112285605.groovy:22)
    at com.kms.katalon.core.main.ScriptEngine.run(ScriptEngine.java:194)
    at com.kms.katalon.core.main.ScriptEngine.runScriptAsRawText(ScriptEngine.java:119)
    at com.kms.katalon.core.main.TestCaseExecutor.runScript(TestCaseExecutor.java:337)
    at com.kms.katalon.core.main.TestCaseExecutor.doExecute(TestCaseExecutor.java:328)
    at com.kms.katalon.core.main.TestCaseExecutor.processExecutionPhase(TestCaseExecutor.java:307)
    at com.kms.katalon.core.main.TestCaseExecutor.accessMainPhase(TestCaseExecutor.java:299)
    at com.kms.katalon.core.main.TestCaseExecutor.execute(TestCaseExecutor.java:233)
    at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:114)
    at com.kms.katalon.core.main.TestCaseMain.runTestCase(TestCaseMain.java:105)
    at com.kms.katalon.core.main.TestCaseMain$runTestCase$0.call(Unknown Source)
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:149)
    at TempTestCase1564113319081.run(TempTestCase1564113319081.groovy:21)
    at groovy.lang.GroovyShell.runScriptOrMainOrTestOrRunnable(GroovyShell.java:263)
    at groovy.lang.GroovyShell.run(GroovyShell.java:518)
    at groovy.lang.GroovyShell.run(GroovyShell.java:507)
    at groovy.ui.GroovyMain.processOnce(GroovyMain.java:653)
    at groovy.ui.GroovyMain.run(GroovyMain.java:384)
    at groovy.ui.GroovyMain.process(GroovyMain.java:370)
    at groovy.ui.GroovyMain.processArgs(GroovyMain.java:129)
    at groovy.ui.GroovyMain.main(GroovyMain.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.codehaus.groovy.tools.GroovyStarter.rootLoader(GroovyStarter.java:109)
    at org.codehaus.groovy.tools.GroovyStarter.main(GroovyStarter.java:131)
    java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5433

i got it fixed by myself :slight_smile:
working object codes
@Keyword
public void connectToPostgres(){

	try {
		Class.forName("org.postgresql.Driver")
		String url = "jdbc:postgresql://localhost:5433/postgres?currentSchema=SCHEMA";
		Properties props = new Properties();
		props.setProperty("user","postgres");
		props.setProperty("password","test");
		c = DriverManager.getConnection(url, props);
	} catch (Exception e) {
		e.printStackTrace()
		System.err.println(e.getClass().getName()+": "+e.getMessage())
		System.exit(0)
	}
	System.out.println("Opened database successfully")
}
1 Like