Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

A question

Status
Not open for further replies.

galar

Programmer
Feb 19, 2002
59
0
0
IL
I'm new to Java, so I'll have to ask some stupid quetions in this forum.
This is my first one:
I want to get records from my DB (this one I know how to do) and I need to show them on the screen - here is the problem. How can I make it happen?
For example, I have a Customers table and I want to see this:

Id Name Phone
4 galar 11111111
5 nick 22222222
.. .... .........
.. .... .........

Thanks in advance
 
you should write better subject for your question. "A question" is not very descriptive. Why didn't you say "How can I connect to a database?"

I'm not being picky or anything it's just that people tend to overlook subjects like yours and tend to answer things they know they can answer quickly. Gary Haran
 
if you are talking about console app then just use

System.Out

but if you want to do some formating of the text output, it's a little harder to do in Java. I suggest you use a Swing table...

Dix
 
Use the ResultSet operations of the form getXxx(int index) or getXxx(String colname) where index is the column number.

e.g
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select * from customer");

Note that there is no ; in the query.

I now presume the datatypes are all VARCHAR...these show you the two possible ways of getting the data:

String id = new String(rs.getString(1));
String name = new String(rs.getString("Name");

If there is a number datatype then then: rs.getInt(1)

If you have a many rows then use

while (rs.next()) {
id = new String(rs.getString(1));
.. add the id to a vector or whatever

}
 
I'll shime in to recommend using the field names rather than the indexes. If you later decide to change your query, whether adding or removing fields, you will have less work maintaining your code. Laziness is a virtue!
 
good point, now that's what a real programmer would do. While we are at it, may I suggest stored proc data access and/or resource bundle storage of sql str.

Dix
 
Thanks to all of you.
Ok, I get the idea.
mcowen, I was doing just the same, but it seemed to me that there's another way to do this. Maybe, it's because I'm working with VBA right now and the conseption is very different. I would like to build the same application that I've made in VBA with Java. So, here comes another question:
How is better to design it? I liked Swing very much, but I heard that it can create some problems. Can someone tell me more about it?

Thanks.
 
what kind of problems? Swing will work fine on almost all computers today. It has a pluggable look and feel. It's can multithread using swing workers. It can be converted to an applet easily for web delopyment. Or if you want, a computer with Java Web Start can download the program on the fly. It has great exception handling ( no more "on error resume next"). You can package your program with an installer like the zeroG installer for on the fly jre download. It supports resizing. Can VB app's wigets resize, I think not (unless you hack it with code). Trust me you can't go wrong.

Dix
 
Well, I know the advantages of Java on VB, beleive me. It based on a little experiance.
The question was is what better to use when designing the application in Java - the regular GUI or the Swing?
 
from my little knowledge of JAVA (probably im stating the obvious hehe) :
the regualr GUI is faster, as it takes components, etc. directly from the operating system.
Swing is completely jvm-generated so it's a bit slower, but it was created after awt so it's kind of cooler. at least for a beginner like me it tends to be easier to use, it has a lot more functionality built-in. as far as stability goes, it's great. it's a very 'modular' interface builder, i love it. Just make sure u dont mix awt and swing hehe ;)

hope that helps a little.
Avendeval


 
I like Swing too. Slowleness is an important factor, but it does look "cooler" and easier for use. I think I'll go for it.
Thanks to all of you for answering.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top