I am trying to make a Java EE project with SQL as my database and glassfish as my server. I want to retrieve all the data in my table in to a html page but I have to use servlet and session beans. My output will be shown in a html page.
Asked
Active
Viewed 9,467 times
2

Brian Tompsett - 汤莱恩
- 5,753
- 72
- 57
- 129

Predator Ajmera
- 25
- 1
- 3
-
Please tell more that have you done already, better with code samples. – Audrius Meškauskas Jan 10 '13 at 16:23
1 Answers
2
An example
<%@ page language="java"%>
<%@ page import = "java.sql.Connection"%>
<%@ page import = "java.sql.DriverManager"%>
<%@ page import = "java.sql.ResultSet"%>
<%@ page import = "java.sql.Statement"%>
<html>
<body>
<h1>Retrieve data</h1>
<%
try
{
Class.forName("org.gjt.mm.mysql.Driver");
Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/db", "user", "pass");
if (!conexion.isClosed())
{
Statement st = conexion.createStatement();
ResultSet rs = st.executeQuery("select * from contact");
out.println("<table border=\"1\"><tr><td>Id</td>< td>Name</td><td>LastName</td><td>Phone</td></tr>");
while (rs.next())
{
out.println("<tr>");
out.println("<td>"+rs.getObject("id")+"</td>");
out.println("<td>"+rs.getObject("Name")+"</td>");
out.println("<td>"+rs.getObject("LastName")+"</td>");
out.println("<td>"+rs.getObject("Phone")+"</td>");
out.println("</tr>");
}
out.println("</table>");
conexion.close();
}
else
out.println("fail");
}
catch (Exception e)
{
out.println("Exception " + e);
e.printStackTrace();
}
%>
</body>
</html>

ronpy
- 262
- 1
- 13
-
thanks alotbro. thats exactlywhat i waslooking for. i tried on every othe web site. thanks a lot :) – Predator Ajmera Jan 11 '13 at 05:23
-
next query: i have a column in my sql table i want to sum that column and show it on my html page. the table i am talking about is used for storing billing information. and i would like to give a sum of all the rates. please help.!:) – Predator Ajmera Jan 11 '13 at 05:27
-
This might not be relevant to you right now but it can help someone with the same question. To count, you can simply use the `count` method in mysql. for example `SELECT COUNT(*) FROM contacts;` This will give you one value count, I t has worked for me but I stand to be corrected if I have made a mistake. To sum the values then you can use the `SUM` method for mysql. – Yonela Nuba Jun 12 '20 at 10:35