Submit and vote on feature ideas.

Welcome to the new Parasoft forums! We hope you will enjoy the site and try out some of the new features, like sharing an idea you may have for one of our products or following a category.

Need to connect to DB using Extension tool

Options
kamit2733
kamit2733 Posts: 16

Hi,

I am trying to connect to a Oracle Database using a extension tool for which i am getting an error saying that it was unable to locate the jar i have specified the Jar in preferences in the JDBC configs, yet it was giving error. My idea of using the Extension tool is that i would fetch a random row from the DB and use it for execution once the data from the DB is used it can no longer be used for another test, So the implementations was to generate a random row number add it in the sql query and fetch a fresh data. It would be grateful if someone could hel me out using the zxJDBC function for connecting a Oracle DB.

Thanks

Comments

  • benken_parasoft
    benken_parasoft Posts: 1,230 ✭✭✭
    edited November 2017
    Options

    In order to load JDBC drivers from jars added to the Parasoft Preferences you must use one of the following methods from the public scripting API:

    • com.parasoft.api.SQLUtil.getConnection(String driverName, String url, String user, String password)
    • com.parasoft.api.SQLUtil.getDriver(String driverName)

    Alternatively, please consider not using an Extension tool. Please use the DB Tool whenever possible.

  • kamit2733
    kamit2733 Posts: 16
    Options

    Hello Benken,

    Its in Grovvy right? Can you give me a sample code snippet.

    Thanks

  • benken_parasoft
    benken_parasoft Posts: 1,230 ✭✭✭
    edited November 2017
    Options

    The public scripting API (com.parasoft.api.*) is a java API. All the provided script engines (Jython, Groovy, JavaScript/Nashorn, etc.) are java-based and can call java APIs. Again, you should be using the DB Tool. However, if you feel you must make JDBC API calls from a script then calling the Java methods I mentioned are no different than calling any other Java methods.

    If you prefer Groovy:

    import com.parasoft.api.*
    import java.sql.*
    
    void doSomething() {
       Connection conn = SQLUtil.getConnection(
                "com.mysql.jdbc.Driver",  // or whatever JDBC driver class you need
                "jdbc:mysql://somehost.mycompany.com:3306/myDatabase",
                "myusername", "mypassword")}
       Statement stmt = conn.createStatement()
       stmt.execute("some SQL statement")
       Statement query = conn.createStatement()
       ResultSet rs = query.executeQuery("some SQL query")
       // do something with result set here
    }
    
  • kamit2733
    kamit2733 Posts: 16
    Options

    Hi benken,

    I tried running the above script but it is failing it says : java.lang.NoClassDefFoundError: oracle/xdb/XML Type

    do we need xdb jar for this?

    Thanks

  • benken_parasoft
    benken_parasoft Posts: 1,230 ✭✭✭
    edited December 2017
    Options

    The script I posted was only an example where I'm using the MySQL driver with a dummy connection URL. However, if you are using Oracle Database driver then you do need to add all the appropriate jars to the Parasoft JDBC driver preferences.

    From "Oracle Type Extensions" in the SOAtest User's Guide:

    For example, Oracle supports oracle.xdb.XMLType. This is available with xdb.jar and xmlparserv2.jar, which are shipped with specific Oracle applications. It is also available from the XML Developer's Kit (XDK) Java, which is available at http://www.oracle.com (click Downloads, click the link for the XML Developer Kit, then select the kit for any platform). Note that this kit contains other files, but you need only the 2 listed above.

    Both of these jars should be added to the JDBC classpath in the Preferences panel (under Parasoft> JDBC Drivers).