The moose likes Struts and the fly likes output sql query in browser with jsp and struts Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Application Frameworks » Struts
Reply Bookmark "output sql query in browser with jsp and struts" Watch "output sql query in browser with jsp and struts" New topic
Author

output sql query in browser with jsp and struts

Anna Kononen
Greenhorn

Joined: Sep 28, 2004
Posts: 4
hello,

i am using jsp, tomcat, and struts. Can someone post a piece of code that would show how to collect the data retrived from database and display this data in browser by sending it to jsp file.

i have the following:

String url = "jdbc atabase:merchant";
String sql = "select m_NAME from merchant0";
Class.forName("oracle.jdbc.driver.OracleDriver");
con = DriverManager.getConnection(url, "username", "password");
stmt = con.createStatement();
rs = stmt.executeQuery(sql);
response.setContentType("text/html");
con.close();

how the jsp file should look to display the results?

thanx in advance
Marc Peabody
pie sneak
Sheriff

Joined: Feb 05, 2003
Posts: 4695

Not to sound rude, but what you are asking for is more than a simple piece of code. It's an entire application.

You will probably want to make some objects out of your data and eventually iterate through them on the jsp.


A good workman is known by his tools.
Kim Lilienfeld
Greenhorn

Joined: Apr 13, 2004
Posts: 25
Hi

The first step that I would do is to create a collection of the information I want to display. So, if for example I wanted to display the First name, Surname and Telephone number of a certain group of people. I would create a java class called PersonVO (VO = value object). This class would contain 3 String variables: firstName, surname and telephoneNumber. I would create getter and setter methods for all 3 variables.

When I retrieved the information from the database I would create a collection of PersonVOs and set all the values in them from the database.
I would then set this collection to the request or the session (depending on what scope I needed), giving it the name "people".

The following is what I would code in my jsp if I wanted to display the information in a table, just printed out.

Note:
1.whereever it says name="people" it must map to whatever name you called your collection on the request/session.
2. The type must be a full path + name of your class that is in the collection (eg. com.testexample.PersonVO)
3. The property must correspond to a getter/setter in the object. It must be the variable name beginning with a small letter. Struts will automatically call getWhatever if you specified property as whatever.
4. The logic:empty and logic:notEmpty is just how I control it if there are no results returned
Ok, here's the code:

<table width="95%" border="0" cellspacing="1" cellpadding="3">

<tr align="center">
<td height="20" colspan="3">People </td>
</tr>

<tr align="center">
<td>First Name</td>
<td>Surname</td>
<td>Telephone Number</td>
</tr>

<logic:notEmpty name="people">

<logic:iterate id="elem" name="people" type="com.mypackage.structure.PersonVO">

<tr>
<td>
<bean:write name="elem" property="firstName"/>
</td>
<td>
<bean:write name="elem" property="surname"/>
</td>
<td>
<bean:write name="elem" property="telephoneNumber"/>
</td>
</tr>
</logic:iterate>
</logic:notEmpty>

<logic:empty name="people">

<tr align="center">

<td colspan="3" class="headtbls">No people found.</td>

</tr>

</logic:empty>
</table>

I hope this helps.
Kim

P.S. This code might not be 100% accurate as I am currently not working in Struts at my current job. If it does throw any wobblies just let me know and I'll help u figure it out.
Anna Kononen
Greenhorn

Joined: Sep 28, 2004
Posts: 4
THank you so much for your posting!
it really helped me a lot
 
 
subject: output sql query in browser with jsp and struts
 
Threads others viewed
Dynamic columns in datagrid
How can read data from DB
display html in jsp using struts action
Using AJAX in Struts.... need steps to Implement..
JSP Alignment
developer file tools