I tried to use Groovy script below from to connect to an Oracle SQL
database:
def connectDB(String dataFile){
//Load driver class for your specific database type
Class.forName("oracle.jdbc.driver.OracleDriver")
String connectionString = "jdbc:sqlite:" + dataFile
if(connection != null && !connection.isClosed()){
connection.close()
}
connection = DriverManager.getConnection(connectionString)
return connection
}
There is sqlite in connection string, but not sure which value should I use there. (I tried jdbc:oracle
too.)
I use following class to establish database connection.
public class sqlconnect {
private static Connection connection = null;
/**
* Open and return a connection to database
* @param dataFile absolute file path
* @return an instance of java.sql.Connection
*/
@Keyword
def connectDB(String dataFile){
//Load driver class for your specific database type
Class.forName("oracle.jdbc.driver.OracleDriver")
String connectionString = "jdbc:sqlite:" + dataFile
if(connection != null && !connection.isClosed()){
connection.close()
}
connection = DriverManager.getConnection(connectionString)
return connection
}
/**
* execute a SQL query on database
* @param queryString SQL query string
* @return a reference to returned data collection, an instance of java.sql.ResultSet
*/
@Keyword
def executeQuery(String queryString) {
Statement stm = connection.createStatement()
ResultSet rs = stm.executeQuery(queryString)
return rs
}
@Keyword
def closeDatabaseConnection() {
if(connection != null && !connection.isClosed()){
connection.close()
}
connection = null
}
/**
* Execute non-query (usually INSERT/UPDATE/DELETE/COUNT/SUM...) on database
* @param queryString a SQL statement
* @return single value result of SQL statement
*/
@Keyword
def execute(String queryString) {
Statement stm = connection.createStatement()
boolean result = stm.execute(queryString)
return result
}
}
I already set database information under Project > Settings > Database
in Katalon Studio.
I call from testcase with CustomKeyword connectDB()
and executeQuery()
methods.
UPDATE:
I updated connectDB()
method Groovy
script:
def connectDB(){
Class.forName("oracle.jdbc.driver.OracleDriver")
//String connectionString = "jdbc:oracle:thin:username/password@ipaddress:port/servicename"
if(connection != null && !connection.isClosed()){
connection.close()
}
connection = DriverManager.getConnection("jdbc:oracle:thin:username/password@ipaddress:port/servicename", "username", "password")
return connection
}
I tried to use variable connectionString
as a parameter of DriverManager.getConnection()
method, but I got same error message in both cases.
Cannot cast object 'oracle.jdbc.driver.T4CConnection@' with class 'oracle.jdbc.driver.T4CConnection' to class 'com.mysql.jdbc.Connection'