How can I return values for 2 columns in my database?



  • 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;
        }
    


  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2