Search code examples
javaarraysdatabaserowset

how to access returned results from rowSet in Java


I'm trying to access what the rowSet variable has returned i.e. it is retreving everything from properties where two conditions are met, BUT:

        JdbcRowSetImpl rowSet = new JdbcRowSetImpl();
        int price = 300000;
        rowSet.setUrl("jdbc:mysql://localhost:3306/litrealty");
        rowSet.setUsername("root");
        rowSet.setPassword("");
        rowSet.setCommand("SELECT * FROM properties WHERE city = ? AND price < ?");
        rowSet.setString(1, l);
        rowSet.setInt(2, price);

        rowSet.execute();

since this piece of code is retreving these informations from the database how can I access it and i.e. put into an array so I can scroll through it then via next/previous buttons?


Solution

  • you can use while loop and iterate on your rowset and get the data

    it would look something like this:

    //Creating and Executing RowSet  
    JdbcRowSet rowSet = RowSetProvider.newFactory().createJdbcRowSet();  
    rowSet.setUrl("jdbc:mysql://localhost:3306/litrealty");  
    rowSet.setUsername("root");  
    rowSet.setPassword("password");  
    
    rowSet.setCommand("select Id, Name, Salary from employee");  
    rowSet.execute();
    
    while (rowSet.next()) {
        System.out.println("Id: " + rowSet.getString(1));  
        System.out.println("Name: " + rowSet.getString(2));  
        System.out.println("Salary: " + rowSet.getString(3));  
    }
    

    Instead i would suggest you to use PreparedStatement to fetch data base from database