Link to home
Start Free TrialLog in
Avatar of coreJ
coreJ

asked on

selecting data from a database

I am using the following code to get all information in the table from a database.
I want to extract everything and append to a JTextArea already in a panel.
Does ResultSet provide such capability ?How best do i solving this problem?

public void selectData(String databaseTable) throws SQLException {
String Database=databaseTable;
String get_out=" SELECT * FROM" + Database;
                    
      stat=con.createStatement();
      ResultSet rs=stat.executeQuery(get_out);
...
...
..
                rs.close ();
}
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You'll need to get the individual fields and append them unfortunately
Avatar of aozarov
aozarov

>> String get_out=" SELECT * FROM" + Database;
String get_out=" SELECT * FROM " + TableName;

ResultSet rs=stat.executeQuery(get_out);
...
while (rs.next())
{
rs.getString(1)
or
rs.getInt(1)
or basically
rs.getXXX(index - starts from 1) XXX  = type

}
see: http://java.sun.com/j2se/1.4.2/ docs/api/java/sql/ResultSet.html  for api details
or http://www.javaalmanac.com/cgi-bin/search/find.pl?words=ResultSet for examples
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of coreJ

ASKER

Too bad.
Handling individual fields means i need to know the structure of the database table which is what i want to avoid.is not possible to just supply the name of the table and voila one can dump everything to a textarea somehow ?
>>Handling individual fields means i need to know the structure of the database table

Not so. See the code i just posted
Avatar of coreJ

ASKER

Yes CEHJ your code seems likely a solution to my question, but will this extract columnNames of as well since i see only columnCount here ?
Yes, getColumnName(index)
Avatar of coreJ

ASKER

to make myself clear:
 what does rs.getObject(i) return CEHJ ?
If you want, yes:

for (int column = 0; column < numberOfColumns; column++) {
      ta.append(" " + metaData.getColumnLabel(column + 1));
}
>>what does rs.getObject(i) return CEHJ ?

Whatever's in the field effectively
Can I answer as well?
rs.getObject(i) return value depends on the type of the column. see: http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html
you cal also call rs.getString(i) which will always return a string (by converting the specific type to a string)
Avatar of coreJ

ASKER

will this not overwrite the columns as i loop through the numberOfColumns ?
That last code i posted would be followed by

ta.append("\n");

then the original code
>> will this not overwrite the columns as i loop through the numberOfColumns ?

No
Avatar of coreJ

ASKER

In effect you mean something like this:

ResultSetMetaData metaData = rs.getMetaData();
for (int column = 0; column < numberOfColumns; column++) {
     ta.append(" " + metaData.getColumnLabel(column + 1));
}
ta.append("\n");
int numberOfColumns = metaData.getColumnCount();
while (rs.next()) {
     for (int i = 1; i <= numberOfColumns; i++) {
          ta.append(" " + rs.getObject(i));
     }
     ta.append("\n");
}

Thanks but my problem is whether this code will not in effect reduce to only one columnName and on  object on the textarea since ta.append() will continuously append to the same location in the textarea ??
>>int numberOfColumns = metaData.getColumnCount();

should come first

>> will continuously append to the same location in the textarea ??

No - it won't

append(String)

"Appends the given text to the end of the document"
Avatar of coreJ

ASKER

Yeah thanks for the correction, i was just trying to rearrange without giving much thought to it.
Thanks for your help CEHJ.
I trust you so I give the points to you.Will try the code out later actually.
:-) You can come back if it doesn't ;-)
Avatar of coreJ

ASKER

will surely do so ;-)
thanks also to you aozarov. the points rather unfortunately goes to CEHJ.
thank you all.
:-)
That is fine :-)
Just note my corrections and comments:

>> String get_out=" SELECT * FROM" + Database;
String get_out=" SELECT * FROM " + TableName;

Be aware of the mapping http://java.sun.com/j2se/1.3/docs/guide/jdbc/getstart/mapping.html if calling getObject
And probably calling getString will be a better choice for you as you need them as strings (adding it the the StringBuffer)
Avatar of coreJ

ASKER

Thats very commendable aozarov !!! Thank you very much. Actually i'm considering using getxxxxx aferall. :-)