在Katalon Studio中连接到Oracle SQL

格子衬衫

我尝试使用以下from的Groovy脚本连接Oracle SQL数据库:

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
}

连接字符串中包含sqlite,但不确定在其中应使用哪个值。(我也尝试jdbc:oracle过。)

我使用以下类建立数据库连接。

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
    }
}

我已经Project > Settings > Database在Katalon Studio中设置了数据库信息我用CustomKeywordconnectDB()executeQuery()方法从测试用例中调用

更新:

我更新了connectDB()方法Groovy脚本:

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
}

我尝试将变量connectionString用作DriverManager.getConnection()方法的参数,但在两种情况下都得到了相同的错误消息。

无法将类别为'oracle.jdbc.driver.T4CConnection'的对象'oracle.jdbc.driver.T4CConnection @'转换为类'com.mysql.jdbc.Connection'

胡安·梅拉多

连接字符串的格式为jdbc:oracle:<drivertype>:@<database>(例如jdbc:oracle:thin:@host:1521:xe)。

您可以在call:DriverManager.getConnection("<connection string>", "<user>", "<password>");甚至在连接字符串中传递用户名和密码jdbc:oracle:<drivertype>:<user>/<password>@<database>(例如jdbc:oracle:thin:scott/tiger@host:1521:xe)。

您应该了解有关连接字符串的更多信息。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章