1

thanks for reading.

My scenario (possibly not the best way of doing things, but im still learning)

I have a sql database, which houses in 1 table test data (A string value) and a boolean (expected to pass or fail)

Table:

I am trying to grab the resultset results here for both columns inside one method, should I be storing both columns in seperate arraylists? is there something simpler.

I've dabbled with storing the one row but obviously thats not fit for purpose, I need both these values together which I can then return somehow in order to run a test which will loop through every entry in that table

Here is my basic code so far, it will need a lot of modifications for sure (Ive been messing around with arraylists etc

public class TD_RestrictedInternetItems_CategoryAddData {


    private String Values;
    private boolean PassTrue;

    public void getTestData() {
        Connection conn = null;
        ResultSet result = null;
        String url = "jdbc:jtds:sqlserver://localhost/master;instance=TESTDATA;user=sa";
        String driver = "net.sourceforge.jtds.jdbc.Driver";


        try
        {
            // import driver
            Class.forName(driver);
            // connection to database
            conn = DriverManager.getConnection(url);
            Statement state = conn.createStatement();
            String sql = "SELECT TOP 1 * FROM TestData.dbo.RestrictedInternetAccess_AddCategoryTD";
            result = state.executeQuery(sql);

            while(result.next()) {  
            Values = result.getString("TestData_Value");
            PassTrue = result.getBoolean("TestWillPass");

            }

        }


        catch (SQLException e)
        {
            e.printStackTrace();
        }

        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        finally
        {
            try {
                result.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }
            try {
                conn.close();
            } catch (SQLException e) {

                e.printStackTrace();
            }

        }                   
    }


    public String getData() {
        return Values;
    }

    public Boolean getPassOrFail() {
        return PassTrue;
    }
2
  • Which test framework are you using to run your Selenium tests in?
    – FDM
    Jul 4, 2016 at 7:28
  • Nothing in the question or the code fragment refers to Selenium.
    – user246
    Nov 2, 2016 at 18:56

2 Answers 2

2

This is common problem, usually solved by object-relation mapper (ORM) helper library. There are many for Java (too many), so it is not easy to choose a good one. :-) Do your research before you commit to one of them.

0

Since this is more of a "what works best for you answer" I will provide a solution on how our team handled something like this in our testing framework. (Feel free to disregard if it doesn't work for you)

We decided that we wanted to use a list of dictionaries to hold our SQL data. This way we could pull as many columns as we wanted, and have as many rows as we needed all within one object. This did require a bit of typing when getting the data within the object, but since we did this within our framework, it was pretty simple.

Please let me know if you'd need anymore clarification.

Not the answer you're looking for? Browse other questions tagged or ask your own question.