944,149 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 5212
  • JSP RSS
Jun 26th, 2007
0

fetching data from the database

Expand Post »
Hi,

I have written a jsp file to fetch data from a database table and display it in the form of a table in jsp. The idea is to generate a dynamic table. I have written the following code.
#
<%@page import="java.sql.*"%>
<html>
<head>
<title>The alert table</title>
</head>
<body>
<h1>Alerts</h1>
<%
Connection conn = null;
ResultSet rs1 = null;
Statement stmt = null;
ResultSetMetaData rsmd = null;
String query="select * from alertdata";

try {
Class c = Class.forName("org.postgresql.driver");
conn = DriverManager.getConnection("jdbc:postgresql://192.168.128.150:5432/thirdeye", "postgres", "postgres");
stmt = conn.createStatement();
rs1 = stmt.executeQuery(query);

int columns=0;
rsmd = rs1.getMetaData();
columns = rsmd.getColumnCount();
}
catch (SQLException e) {
System.out.println("Error occurred " + e);
}
%>
<table width="90%" border="1">
<tr>
<% // for the header cells
try {
for (int i=1; i<=columns; i++) {
out.write("<th>" + rsmd.getColumnLabel(i) + "</th>");
}
%>
</tr>


<% // for each row in the database table
while (rs1.next()) {
out.write("<tr>");
for (int i=1; i<=columns; i++) {
out.write("<td>" + rs1.getString(i) + "</td>");
}
out.write("</tr>");
}

// close the connection and the statement
stmt.close();
conn.close();
}
catch (SQLException e) {
System.out.println("Error " + e);
}

finally {
try {
if (stmt != null)
stmt.close();
} catch (SQLException e) {}
try {
if (conn != null)
conn.close();
} catch (SQLException e) {}
}

%>
</table>
</body>
</html>
#

However this gives me an error, cannot resolve columns.

Please help.
Saswati
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saswati_mishra is offline Offline
15 posts
since Jun 2007
Jun 26th, 2007
0

Re: fetching data from the database

You have defined 'columns' locally inside a try-catch block and are attempting to use it in another block. Move the declaration outside the first block so that it is visible to the second.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Jun 27th, 2007
0

Re: fetching data from the database

Click to Expand / Collapse  Quote originally posted by Ezzaral ...
You have defined 'columns' locally inside a try-catch block and are attempting to use it in another block. Move the declaration outside the first block so that it is visible to the second.
My code is working now. Thanks a lot for your help.

Saswati
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saswati_mishra is offline Offline
15 posts
since Jun 2007
Jun 28th, 2007
0

Re: fetching data from the database

You aware that accessing any database with JSP is not recommended and should be avoid. Servlets been design for this task...
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Jun 28th, 2007
0

Re: fetching data from the database

Hi,

I am very new in java/jsp. Could you please explain me how to use servlets instead of jsp.

Thanks a lot in advance.

Saswati
Reputation Points: 10
Solved Threads: 0
Newbie Poster
saswati_mishra is offline Offline
15 posts
since Jun 2007
Jun 28th, 2007
0

Re: fetching data from the database

Story of a Servlet
Basic Servlet Structure also there is more ont that site
Introduction to Servlets

And there many-many more you just need to specify what exactly you are looking for
Moderator
Featured Poster
Reputation Points: 2786
Solved Threads: 874
Code tags enforcer
peter_budo is offline Offline
6,659 posts
since Dec 2004
Jun 28th, 2007
0

Re: fetching data from the database

Also you should consider using JSTL instead of embedding business logic / scriptlets inside your JSP file.

Read the following:
http://java.sun.com/products/jsp/jstl/
http://www.developer.com/java/ejb/article.php/1447551
Last edited by ~s.o.s~; Jun 28th, 2007 at 12:34 pm.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: javascript to jsp
Next Thread in JSP Forum Timeline: JSP and JDBC





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC