954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

fetching data from the database

Alerts


<%
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);
}
%>

<% // for the header cells
try {
for (int i=1; i<=columns; i++) {
out.write("" + rsmd.getColumnLabel(i) + "");
}
%>


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

// 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) {}
}

%>

saswati_mishra
Newbie Poster
15 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
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

saswati_mishra
Newbie Poster
15 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

You aware that accessing any database with JSP is not recommended and should be avoid. Servlets been design for this task...

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

saswati_mishra
Newbie Poster
15 posts since Jun 2007
Reputation Points: 10
Solved Threads: 0
 

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

peter_budo
Code tags enforcer
Moderator
15,436 posts since Dec 2004
Reputation Points: 2,806
Solved Threads: 902
 

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

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You