| | |
fetching data from the database
Please support our JSP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
•
•
Join Date: Jun 2007
Posts: 15
Reputation:
Solved Threads: 0
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
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
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
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)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Publilius Syrus
(~100 BC)
LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
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
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.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- Writing to an Access Database (Visual Basic 4 / 5 / 6)
- inser data into database (JSP)
- Validating data in a field to a existing data stored in the database (PHP)
- insert data from one form into two diffent database table (JSP)
- how to insert data from dataset into database (VB.NET)
- IIS connection will not write data (ASP)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
- How to incorporate Database into C++ ? (C++)
Other Threads in the JSP Forum
- Previous Thread: javascript to jsp
- Next Thread: JSP and JDBC
| Thread Tools | Search this Thread |
apache backbutton combobox connection database development directorystructure dynamicpagetitles eclipse frames glassfish ie8 imagetodatabse imageupload integer internet java javaee javascript jsf jsp jsppagetitles levels mvc2 mvcmodel2 network parameters passing ping printinserverinsteadofclient redirect request.getparameter response servlet servletdopost()readxml sessions software ssl state_saving_method stocks sun tomcat tutorial update video web






