Hi,
Im trying to display all the rows from a table (im running on Postgres and NetBeans 6.5). I only get one row..
my code:
the actor class
package appsClass;
import java.sql.*;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
*/
public class Actor
{
private int actor_id;
private String lastname;
private String firstname;
public void setActor_id(int actor_id)
{
this.actor_id = actor_id;
}
public int getActor_id()
{
return (this.actor_id);
}
public void setLastname(String lastname)
{
this.lastname = lastname;
}
public String getLastname()
{
return (this.lastname);
}
public void setFirstname(String firstname)
{
this.firstname = firstname;
}
public String getFirstname()
{
return (this.firstname);
}
public Actor (ResultSet resultSet) throws SQLException
{
this.actor_id = resultSet.getInt("actor_id");
this.lastname = resultSet.getString("lastname");
this.firstname = resultSet.getString("firstname");
}
}
// Servlet
package servlet;
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import dbconn.dbconn;
import appsClass.Actor;
public class actors extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String qryStr = "";
Statement stmt = null;
ResultSet rs = null;
// res.setContentType("text/html");
PrintWriter out = res.getWriter();
Connection conn = null;
//using movie_resolved view
try{
// Get a Connection to the database
conn = dbconn.getJDBCConnection();
// Create a Statement object
stmt = conn.createStatement();
// Execute an SQL query, get a ResultSet
qryStr = "SELECT * from actor";
rs = stmt.executeQuery(qryStr);
if (rs.next())
{
Actor actor = new Actor (rs);
actor.setActor_id(rs.getInt("actor_id"));
actor.setLastname(rs.getString("lastname"));
actor.setFirstname(rs.getString("firstname"));
req.setAttribute("actor", actor);
RequestDispatcher view = req.getRequestDispatcher("actor.jsp");
view.forward(req, res);
}
}
catch(ClassNotFoundException e) {
out.println("Couldn't load database driver: " + e.getMessage());
}
catch(SQLException e) {
out.println("SQLException caught: " + e.getMessage());
}
finally {
// Always close the database connection.
try {
if (conn != null) conn.close();
}
catch (SQLException ignored) { }
}
}
}
the JSP (for views)
<%--
--%>
<%@ page language="java" %>
<%@ page import ="java.sql.*;"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Actor</title>
</head>
<body>
<h2>This is Actor Class</h2>
<table>
<TR>
<TD>Actor ID: </TD>
<TD>${actor.actor_id}</TD>
</TR>
<TR>
<TD>Lastname: </TD>
<TD>${actor.lastname}</TD>
</TR>
<TR>
<TD>Firstname: </TD>
<TD>${actor.firstname}</TD>
</TR>
</table>
</body>
</html>
I want to display list of user in the table.. but couldnt figure it out.. help me..