Forum Discussion

adymlincoln's avatar
adymlincoln
New Contributor
12 years ago

JDBC Request - Excel data processing

Hi all,

Trying to use the JDBC Request (Test Step), but it keeps returning

2012-09-13 14:14:44 - Error getting response; null

Driver : sun.jdbc.odbc.JdbcOdbcDriver
Connection String : jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=C:\\My Data\\projects\\ao\\uat-data.xls;ReadOnly=0

The Test Connection comes back successful...


In addition, I've verified the Excel data and connection information. I've even written a Groovy Script that is able to read the Excel...

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

def soapUI_Excel = "C:\\My Data\\projects\\ao\\uat-data.xls"
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
soapUIDataConnection = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + soapUI_Excel + ";ReadOnly=0", "", "");
Statement stmt = ((Connection) soapUIDataConnection).createStatement();
recordSet = stmt.executeQuery("Select * from [Sheet1\$] where case like '2.11%'");
//recordSet = stmt.executeQuery("Select * from [Sheet1\$]");

while ( recordSet.next() ) {
log.info(recordSet.getString("logical-name"))
}

Has anyone successfully gotten a JDBC Request to work?


tia,

adym

2 Replies

  • Yes. I'm already doing this...I have two Groovy Scripts, one reads from the Excel, the other writes to the Excel.

    What I'm trying to do is replicate that behavior using the JDBC Request Test Step(s)...Not everyone in my group knows (or likes) Groovy scripting...

    Here's what I have so far:

    read-uat-data - Groovy

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    //
    // SCRIPT : read-uat-data
    // DATE : 09/13/2012
    // DEVELOPER : Adym S. Lincoln
    // PURPOSE :
    // Will open and read an Excel spreadsheet file using JDBC:ODBC drivers. It
    // assumes the Excel Spreadsheet has a key column named tc (test case). It
    // picks up the key value from the local soapUI Test Case tc-properties file,
    // tc-properties#tc.
    //
    // It then reads that row from the Excel and populates the soapUI Test Case
    // Properties in tc-properties...
    //

    // KEY : Pull the key value from the local properties Test Step...
    def keyValue = context.expand( '${tc-properties#tc}' );

    // EXCEL : Where is the Excel file located, fully qualified path...
    //def xlsFile = "C:\\My Data\\projects\\ao\\uat-data.xls"
    def xlsFile = context.expand( '${tc-properties#file}' );

    // JDBC : Defined and instantiate tje JDBC:ODBC driver...
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    // JDBC : Define the connection string...
    Connection jdbcConnection = null
    // STATEMENT : Create a database statement object...
    Statement stmt = null
    // READ : Read in the record from the Excel file...if any...
    ResultSet recordSet = null

    try {
    // JDBC : Define the connection string...
    jdbcConnection = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + xlsFile + ";ReadOnly=0", "", "");
    // STATEMENT : Create a database statement object...
    stmt = ((Connection) jdbcConnection).createStatement();
    // READ : Read in the record from the Excel file...if any...
    recordSet = stmt.executeQuery("Select * from [Sheet1\$] where tc like '" + keyValue + "%'");
    //recordSet = stmt.executeQuery("Select * from [Sheet1\$]");

    def llngRows = 0

    while ( recordSet.next() ) {
    log.info(recordSet.getString("tc") + " - " + recordSet.getString("ticket"))
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "logical-name", recordSet.getString("logical-name") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "asset-id", recordSet.getString("asset-id") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "office-id", recordSet.getString("office") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "subject", recordSet.getString("subject") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "priority", recordSet.getString("priority") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "ticket-ebond", recordSet.getString("ebond-ticket") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "open-comment", recordSet.getString("open-comment") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "resolve-comment", recordSet.getString("resolve-comment") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "comment-comment", recordSet.getString("comment-comment") )
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "escalate-comment", recordSet.getString("escalate-comment") )
    llngRows++
    }

    if ( llngRows == 0 ) {
    log.warn ( "No records found for Test Case ID [" + keyValue + "]" )
    }
    testRunner.testCase.testSteps["tc-properties"].setPropertyValue( "rowcount", llngRows.toString() )
    }
    catch (ltheXcp) {
    log.warn ( ltheXcp )
    }
    finally {
    recordSet.close();
    stmt.close();
    jdbcConnection.close();
    }


    write-uat-data - Groovy

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.ResultSet;
    import java.sql.Statement;

    //
    // SCRIPT : write-uat-data
    // DATE : 09/13/2012
    // DEVELOPER : Adym S. Lincoln
    // PURPOSE :
    // Will open and write to an Excel spreadsheet file using JDBC:ODBC drivers. It
    // assumes the Excel Spreadsheet has a key column named tc (test case). It
    // picks up the key value from the local soapUI Test Case tc-properties file,
    // tc-properties#tc.
    //
    // It then write data to that row as needed...you supply the UPDATE statement...
    //

    // KEY : Pull the key value from the local properties Test Step...
    def keyValue = context.expand( '${tc-properties#tc}' );

    // EXCEL : Where is the Excel file located, fully qualified path...
    //def xlsFile = "C:\\My Data\\projects\\ao\\uat-data.xls"
    def xlsFile = context.expand( '${tc-properties#file}' );

    def ticketNumber = context.expand( '${tc-properties#ticket-lmig}' );

    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    Connection jdbcConnection = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + xlsFile + ";ReadOnly=0", "", "");
    Statement jdbcStatement = ((Connection) jdbcConnection).createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);

    q2 = "update [Sheet1\$] set [ticket] = '" + ticketNumber + "' where tc like '" + keyValue + "%'"
    jdbcStatement.executeUpdate(q2)

    jdbcStatement.close();
    jdbcConnection.close();