Sorry if this sounds very basic, but I'm writting a servlet that will connect to a database and show results of queries on a web page. What is the code to display the result set? Do I need to have a java server page that calls the servlet? Annette
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12268
1
posted
0
You can do everything from within a servlet. Don't try to get fancy. If you have not done any database work before, I suggest you try to get the query and response handling working with a regular Java application first, then move to servlets. Output of results is just printing to an output stream, but the composition of the page will have to use HTML of course. Bill
Is it necessary to use metadata if I will only be dealing with one database? or is this the best way to do it. Annette
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
The ResultSetMetaData object gives you information about the ResultSet. In the example, it tells us how many columns are in the ResultSet and the names of the columns. Therefore, the example above is actually reusable since it can be used with any ResultSet.
Annette L'Heureux
Ranch Hand
Joined: Dec 07, 2000
Posts: 135
posted
0
Thanks! I'll try it and let you know. Annette
Annette L'Heureux
Ranch Hand
Joined: Dec 07, 2000
Posts: 135
posted
0
Ok, I've tried it and I need to declare out (I'm assuming it's the PrintWriter). How do I do that? The only code I've seen with that in it has a doGet method, and everything is done in that method. Does this sound familiar? annette
Bosun Bello
Ranch Hand
Joined: Nov 06, 2000
Posts: 1506
posted
0
Annette, Yes, out could be printWriter as in... PrintWriter out = response.getWriter() response is the servlet's response. It is used to return data to the client. Bosun
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Correct. The code above would be in a service(), doGet(), or doPost() method. The "out" represents the response.getWriter() just as Bosum said. You could take the above code and move it to a separate class and have it return a String or StringBuffer instead of writing to out. I will leave that as an excercise for anyone interested. i.e. create a method with a signature of: public static StringBuffer buildResultGrid(ResultSet rs)
Annette L'Heureux
Ranch Hand
Joined: Dec 07, 2000
Posts: 135
posted
0
ok, this is starting to make my brain hurt! Doesn't anyone have a simple example of a servlet that accesses a database and displays the results of a simple sql statement (select * from <table> ) on a web page? I don't imagine it should be that complicated, but when you've never done it before and the only resources you have are a couple of books and this website, it can be pretty overwhelming (especially when your boss would like to see something soon!). Anyway, one day I'll get there. So basically I call doGet() from main() which is supposed to display the results of my query?
Thomas Paul
mister krabs
Ranch Hand
Joined: May 05, 2000
Posts: 13974
posted
0
Notes: there is no main() method in a servlet. Servlets are executed through the web server/servlet engine.
[This message has been edited by Thomas Paul (edited December 08, 2000).]