Listing 5. showblog.jsp
<%@ page language="java" contentType="text/html" %>
<%@ page import="java.sql.*" %>
<HTML>
<Head>
<Title>Weblog</Title>
</Head>
<Body>
<H1>Weblog</H1>
<%
// Some private variables for logging into the
// database
String loginJdbc = "org.postgresql.Driver";
String loginUser = "reuven";
String loginPasswd = "";
String loginUrl = "jdbc:postgresql://localhost/atf";
// Load the PostgreSQL driver
Class.forName(loginJdbc);
Connection con = DriverManager.getConnection
(loginUrl, loginUser, loginPasswd);
// Declare our statement
Statement statement = con.createStatement();
// Make sure that only one thread handles this
// statement at a time
String query = "SELECT entry_date,
entry_headline, ";
query += " entry_text ";
query += "FROM BlogEntries ";
query += "ORDER BY entry_date DESC ";
// Perform the query
ResultSet rs = statement.executeQuery(query);
%>
<table>
<% while (rs.next()) { %>
<tr>
<td> <%= rs.getString("entry_date") %> </td>
<td> <%= rs.getString("entry_headline") %> </td>
<td> <%= rs.getString("entry_text") %></td>
</tr>
<% } %>
</table>
</Body>
</HTML>
Copyright © 1994 - 2018 Linux Journal. All rights reserved.