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_o ut);
...
...
..
rs.close ();
}
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_o
...
...
..
rs.close ();
}
You'll need to get the individual fields and append them unfortunately
>> String get_out=" SELECT * FROM" + Database;
String get_out=" SELECT * FROM " + TableName;
ResultSet rs=stat.executeQuery(get_o ut);
...
while (rs.next())
{
rs.getString(1)
or
rs.getInt(1)
or basically
rs.getXXX(index - starts from 1) XXX = type
}
String get_out=" SELECT * FROM " + TableName;
ResultSet rs=stat.executeQuery(get_o
...
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/ResultSe t.html for api details
or http://www.javaalmanac.com/cgi-bin/search/find.pl?words=ResultSet for examples
or http://www.javaalmanac.com/cgi-bin/search/find.pl?words=ResultSet for examples
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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
Not so. See the code i just posted
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)
ASKER
to make myself clear:
what does rs.getObject(i) return CEHJ ?
what does rs.getObject(i) return CEHJ ?
If you want, yes:
for (int column = 0; column < numberOfColumns; column++) {
ta.append(" " + metaData.getColumnLabel(co lumn + 1));
}
for (int column = 0; column < numberOfColumns; column++) {
ta.append(" " + metaData.getColumnLabel(co
}
>>what does rs.getObject(i) return CEHJ ?
Whatever's in the field effectively
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)
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)
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
ta.append("\n");
then the original code
>> will this not overwrite the columns as i loop through the numberOfColumns ?
No
No
ASKER
In effect you mean something like this:
ResultSetMetaData metaData = rs.getMetaData();
for (int column = 0; column < numberOfColumns; column++) {
ta.append(" " + metaData.getColumnLabel(co lumn + 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 ??
ResultSetMetaData metaData = rs.getMetaData();
for (int column = 0; column < numberOfColumns; column++) {
ta.append(" " + metaData.getColumnLabel(co
}
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
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"
"Appends the given text to the end of the document"
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.
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 ;-)
ASKER
will surely do so ;-)
thanks also to you aozarov. the points rather unfortunately goes to CEHJ.
thank you all.
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)
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)
ASKER
Thats very commendable aozarov !!! Thank you very much. Actually i'm considering using getxxxxx aferall. :-)