User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JSP section within the Web Development category of DaniWeb, a massive community of 426,970 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,469 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JSP advertiser: Lunarpages JSP Web Hosting
Views: 2397 | Replies: 6 | Solved
Reply
Join Date: Jun 2007
Posts: 15
Reputation: saswati_mishra is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
saswati_mishra saswati_mishra is offline Offline
Newbie Poster

fetching data from the database

  #1  
Jun 26th, 2007
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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2007
Location: USA
Posts: 2,847
Reputation: Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all Ezzaral is a name known to all 
Rep Power: 12
Solved Threads: 283
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Posting Maven

Re: fetching data from the database

  #2  
Jun 26th, 2007
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.
Reply With Quote  
Join Date: Jun 2007
Posts: 15
Reputation: saswati_mishra is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
saswati_mishra saswati_mishra is offline Offline
Newbie Poster

Re: fetching data from the database

  #3  
Jun 27th, 2007
Originally Posted by Ezzaral View Post
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
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,467
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: fetching data from the database

  #4  
Jun 28th, 2007
You aware that accessing any database with JSP is not recommended and should be avoid. Servlets been design for this task...
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jun 2007
Posts: 15
Reputation: saswati_mishra is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
saswati_mishra saswati_mishra is offline Offline
Newbie Poster

Re: fetching data from the database

  #5  
Jun 28th, 2007
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
Reply With Quote  
Join Date: Dec 2004
Location: London or Slovakia
Posts: 2,467
Reputation: peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough peter_budo is a jewel in the rough 
Rep Power: 11
Solved Threads: 296
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: fetching data from the database

  #6  
Jun 28th, 2007
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
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

If we helped you to solve your problem, answered your question please mark your post as SOLVED.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,863
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: fetching data from the database

  #7  
Jun 28th, 2007
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 11:34 am.
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JSP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

All times are GMT -4. The time now is 1:24 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC