Connect to Oracle SQL in Katalon Studio

2019-04-28 21:20发布

问题:

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'

回答1:

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.