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 391,628 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,780 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: 2207 | Replies: 6 | Solved
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  

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

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

 

DaniWeb JSP Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the JSP Forum

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