7

我尝试使用下面的 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”

4

1 回答 1

3

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

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

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

于 2018-03-19T12:19:48.353 回答