Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

howto display data from database into html

 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, good day, can someone guide me howto get data from DB and use retreived data to generate table in html form ?

i would rather appreciated if you can show me how to generate table by using javabeans <jsp:usebean><jsp:setProperty><jsp:getProperty>

i have done somethings as follow sample http://www.coderanch.com/t/286085/JSP/java/howto-javabean-create-table-db ..but i don't think that is correct way

thank you very much for your time
 
Jason Milliron
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. If you are using a web app to get data from a DB and then showing the data in an HTML form, use MVC Model 2 design pattern. You can read up about this on the web. Here is a simple example using a Servlet to query teh DB and populate an ArrayList filled with movie java beans. The (Array)List is then added to the request as an attribute and then forwarded to a JSP. The jsp used JSTL to access the (Array)List and display the data. I showed it using EL and getProperty to access the data. I hope this helps. Let me know what you think.

Servlet: QueryServlet.java
/*
* Created on Nov 23, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.db.servlets;

import com.db.beans.*;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* @author none
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class QueryServlet extends HttpServlet {

/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

List actionMovies = populateMovieList("Action");
List comedyMovies = populateMovieList("Comedy");

request.setAttribute("ActionMovies", actionMovies);
request.setAttribute("ComedyMovies", comedyMovies);

RequestDispatcher rd = request.getRequestDispatcher("ViewMovies.jsp");

rd.forward(request, response);

}
/* (non-Javadoc)
* @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1)
throws ServletException, IOException {

doGet(arg0, arg1);

}

private List populateMovieList(String movieType) {

Connection conn = null;
ArrayList movies = new ArrayList();

try {

conn = DriverManager.getConnection("jdbc racle:thin:@<machine name>:<port>:<db _sid>", "username", "password");
Statement stmt = conn.createStatement();
ResultSet rset = stmt.executeQuery("Select id, title, description from videos where section = '" + movieType + "' order by title");

Movie movie = null;

while (rset.next()) {

movie = new Movie();
movie.setId(rset.getInt(1));
movie.setTitle(rset.getString(2));
movie.setDesc(rset.getString(3));

System.out.println("Adding " + movie.getTitle() + " to movie list");

movies.add(movie);
}

rset.close();

} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} finally {

try {
conn.close();
} catch (Exception exp) {}

}

return movies;


}
}

------------------------------------------------------------
JSP: ViewMovies.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<body>
<center>
<b><h2>Movie List</h2></b>
<p>
<table border=2 width="45%">
<tr>
<td colspan="2" align="center"><b>Comedy Movies</b></td>
</tr>
<tr>

<td><b>Title</b></td>
<td><b>Description</b></td>
</tr>
<c:forEach var="comedy" items="${ComedyMovies}" >
<tr>
<td><jsp:getProperty name="comedy" property="title" /></td>
<td><jsp:getProperty name="comedy" property="desc" /></td>
</tr>
</c:forEach>
</table>
<p>
<table border="2" width="45%">
<tr>
<td colspan="2" align="center"><b>Action Movies</b></td>
</tr>
<tr>
<td><b>Title</b></td>
<td><b>Description</b></td>
</tr>
<c:forEach var="action" items="${ActionMovies}" >
<tr>
<td>${action["title"]}</td>
<td>${action["desc"]}</td>
</tr>
</c:forEach>
</table>

</center>
</body>
</html>

------------------------------------------------------------

file: web.xml

?xml version="1.0" encoding="ISO-8859-1"?>

<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4"
>

<display-name>Test DB</display-name>

<session-config>
<session-timeout>1</session-timeout>
</session-config>

<!-- The Usual Welcome File List -->
<welcome-file-list>
<welcome-file>Welcome.jsp</welcome-file>
</welcome-file-list>

<servlet>
<servlet-name>QueryDBServlet</servlet-name>
<servlet-class>com.db.servlets.QueryServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>QueryDBServlet</servlet-name>
<url-pattern>/QueryDB.do</url-pattern>
</servlet-mapping>

</web-app>

-------------------------------------------------------

JavaBean: Movie.java

/*
* Created on Nov 23, 2004
*
* TODO To change the template for this generated file go to
* Window - Preferences - Java - Code Style - Code Templates
*/
package com.db.beans;

/**
* @author none
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class Movie {

private int id;
private String title;
private String desc;

/**
* @return Returns the desc.
*/
public String getDesc() {
return desc;
}
/**
* @param desc The desc to set.
*/
public void setDesc(String desc) {
this.desc = desc;
}
/**
* @return Returns the id.
*/
public int getId() {
return id;
}
/**
* @param id The id to set.
*/
public void setId(int id) {
this.id = id;
}
/**
* @return Returns the title.
*/
public String getTitle() {
return title;
}
/**
* @param title The title to set.
*/
public void setTitle(String title) {
this.title = title;
}
}

----------------
 
Alvin chew
Ranch Hand
Posts: 834
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you very much,Jason Milliron , i think this sample help me a lot ...however i new in web development ..not sure about EL, but definitely this is very good reference , once again , thank you for your help and i really appreciated it
 
steve souza
Ranch Hand
Posts: 862
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the FormattedDataSet at http://www.fdsapi.com. There is a live demo war you can download that allows you to enter a query and have it format the results as a sortable html table. This demo also runs on the internet accessible via the link above. The demo itself does not use MVC, however the FormattedDataSet can work in an MVC environment also.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!