Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

selecting data from a database

Posted on 2005-04-28
24
Medium Priority
?
268 Views
Last Modified: 2010-03-31
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 ();
}
0
Comment
Question by:coreJ
  • 11
  • 8
  • 5
24 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887697
You'll need to get the individual fields and append them unfortunately
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13887702
>> 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

}
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13887718
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Accepted Solution

by:
CEHJ earned 200 total points
ID: 13887730
Something like:


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

Author Comment

by:coreJ
ID: 13887743
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 ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887775
>>Handling individual fields means i need to know the structure of the database table

Not so. See the code i just posted
0
 

Author Comment

by:coreJ
ID: 13887803
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 ?
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13887821
Yes, getColumnName(index)
0
 

Author Comment

by:coreJ
ID: 13887825
to make myself clear:
 what does rs.getObject(i) return CEHJ ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887836
If you want, yes:

for (int column = 0; column < numberOfColumns; column++) {
      ta.append(" " + metaData.getColumnLabel(column + 1));
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887849
>>what does rs.getObject(i) return CEHJ ?

Whatever's in the field effectively
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13887855
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)
0
 

Author Comment

by:coreJ
ID: 13887857
will this not overwrite the columns as i loop through the numberOfColumns ?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887860
That last code i posted would be followed by

ta.append("\n");

then the original code
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887903
>> will this not overwrite the columns as i loop through the numberOfColumns ?

No
0
 

Author Comment

by:coreJ
ID: 13887909
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 ??
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887943
>>int numberOfColumns = metaData.getColumnCount();

should come first

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

No - it won't

0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887953
append(String)

"Appends the given text to the end of the document"
0
 

Author Comment

by:coreJ
ID: 13887975
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.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13887982
:-) You can come back if it doesn't ;-)
0
 

Author Comment

by:coreJ
ID: 13888003
will surely do so ;-)
thanks also to you aozarov. the points rather unfortunately goes to CEHJ.
thank you all.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 13888024
:-)
0
 
LVL 15

Expert Comment

by:aozarov
ID: 13888071
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)
0
 

Author Comment

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month21 days, 6 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question