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