Add a method getListForQuery(String query) to the DatabaseManager class that will return a List of all the columns in the first row returned in the result set for the passed query. You can find out how many columns have been returned by getting a ResultSetMetaData object from the ResultSet object using the method getMetaData() method. You can then ask the ResultSetMetaData object for the number of columns by using the method getColumnCount().

What will be an ideal response?

```
/? ?
? Method t o e x e c u t e a q u e r y and r e t u r n a l i s t o f s t r i n g s
? f o r t h e f i r s t r e t u r n e d row
? @param q u e r y t h e q u e r y t o e x e c u t e
? @return l i s t o f s t r i n g s f o r t h e d a t a i n t h e f i r s t
? r e t u r n e d row , t h e l i s t may be empty
?/
public L i s t g e t L i s t F o r Q u e r y ( S t r i n g query )
{
L i s t r e s u l t L i s t = new A r r a y L i s t ( ) ;
// t r y t h e f o l l o w i n g
try {
// open t h e c o n n e c t i o n t o t h e d a t a b a s e
Connection c o n n e c t i o n =
DriverManager . g e t C o n n e c t i o n ( t h i s . u r l S t r ) ;
// c r e a t e a s t a t e m e n t
Statement s t a t e m e n t = c o n n e c t i o n . c r e a t e S t a t e m e n t ( ) ;
// e x e c u t e t h e q u e r y
R e s u l t S e t r s = s t a t e m e n t . executeQuery ( query ) ;
// f i n d o u t how many columns a r e i n t h e r e s u l t
ResultSetMetaData rsmd = r s . getMetaData ( ) ;
i n t numCols = rsmd . getColumnCount ( ) ;
// p r i n t o u t t h e r e s u l t s
i f ( r s . next ( ) )
{
f o r ( in t i = 1 ; i <= numCols ; i ++)
r e s u l t L i s t . add ( r s . g e t S t r i n g ( i ) ) ;
}
// c l o s e e v e r y t h i n g
rs . close ( ) ;
statement . c l o s e ( ) ;
connection . close ( ) ;
} catch ( SQLException ex ) {
SimpleOutput . showError ( ” Trouble with t h e d a t a b a s e ” + u r l S t r ) ;
ex . p r i n t S t a c k T r a c e ( ) ;
}
return r e s u l t L i s t ;
}
```

Computer Science & Information Technology

You might also like to view...

Which form of online communication happens in real time?

a.Blog b.Chat c.Email d.Forums

Computer Science & Information Technology

What is the second step of the incident response procedure?

A. Detect the incident. B. Report the incident. C. Recover from the incident. D. Respond to the incident.

Computer Science & Information Technology