0
public void Execute_Select_Statement(String instruction) throws SQLException
{
    PreparedStatement ps = null;
    try
    {
        ps = this.conn.prepareStatement(instruction);

        ResultSet rs = ps.executeQuery();

        while (rs.next())
        {
            String column = rs.getString(1);
            System.out.println("column1:" + column);
        }
    }
    catch(SQLException e)
    {
        System.out.println(e.getMessage());
    }
    finally
    {
       if(ps != null)
       {
           ps.close();
       }
    }
}

I now have the code above, which gets a specific column from a table in an oracle database. But when I want to get more columns I have to add another getstring. How can I fix it in a way that it first gets the number of columns of the given table, and then gets the info out of it?

1 Answers1

0

You can use the ResultSetMetaData.getColumnCount() method retrieved from the ResultSet metadata instance:

ResultSetMetaData metadata = rs.getMetaData();
int columnCount = metadata.getColumnCount();
M A
  • 71,713
  • 13
  • 134
  • 174