6

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'

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
plaidshirt
  • 5,189
  • 19
  • 91
  • 181
  • 2
    The format of the connection string is `jdbc:oracle::@` (e.g. `jdbc:oracle:thin:@host:1521:xe`). [Learn more about connection strings...](https://docs.oracle.com/cd/E11882_01/appdev.112/e13995/oracle/jdbc/OracleDriver.html) – Juan Mellado Mar 14 '18 at 19:01
  • @JuanMellado : I fixed this string. I get `Connection succcesful!` message on GUI, but I get `java.sql.SQLException: ORA-01017: invalid username/password; logon denied` error message when call `connectDB()` method. – plaidshirt Mar 19 '18 at 08:09
  • You can pass the user and password on the call: `DriverManager.getConnection ("", "", "");`, or in the connection string: `jdbc:oracle::/@` – Juan Mellado Mar 19 '18 at 12:15
  • 2
    The problem in your latest edit just means you have imported `com.mysql.jdbc.Connection` (which is a MySQL class) instead of the JDBC interface `java.sql.Connection`. Fix your imports. – Mark Rotteveel Mar 20 '18 at 21:05
  • @MarkRotteveel : Thanks, that is solved this issue. – plaidshirt Mar 21 '18 at 10:15

1 Answers1

3

The format of the connection string is jdbc:oracle:<drivertype>:@<database> (e.g. jdbc:oracle:thin:@host:1521:xe).

You can pass the user and password on the call: DriverManager.getConnection("<connection string>", "<user>", "<password>"); or even in the connection string: jdbc:oracle:<drivertype>:<user>/<password>@<database> (e.g. jdbc:oracle:thin:scott/tiger@host:1521:xe).

You should learn more about connection strings.

Juan Mellado
  • 14,973
  • 5
  • 47
  • 54
  • I updated connection string, but got error message: `Cannot cast object 'oracle.jdbc.driver.T4CConnection@' with class 'oracle.jdbc.driver.T4CConnection' to class 'com.mysql.jdbc.Connection'` I use port number and service name to connect to database. – plaidshirt Mar 19 '18 at 15:23
  • Have you tried calling `DriverManager.getConnection` with the user and password as parameters? – Juan Mellado Mar 19 '18 at 15:50
  • My `connectionString` named variable already conations username and password too. I used `connection = DriverManager.getConnection("", "", "");` format, but got the same error message. – plaidshirt Mar 20 '18 at 08:57
  • 1
    Please update your question with the current code you are using. You got a succcesful connection before, only username/password was wrong. But now you are reporting an error about "com.mysql.jdbc.Connection" (mysql?). – Juan Mellado Mar 20 '18 at 10:03
  • I updated it with modified connectDB() method. I call this from test case during execution. – plaidshirt Mar 20 '18 at 10:18
  • Connection wasn't successful from code, only under Project > Settings > Database in Katalon Studio, by Test Connection button. – plaidshirt Mar 20 '18 at 10:20