How to display my ResultSet in a JSP??
First off, I'm new to Java programming.
Here's my problem. I have a set of users in my database. I'd like to retrieve all the user ID's (these are integers) from my User-table and display them in a JSP. Now, I realize that my coding below isn't very efficient as it's not exactly a MVC approach. I'm just trying to learn what a ResultSet is and how to manipulate it.
I send my ResultSet to a JSP by "request.setAttribute("usersId", usersId);" but then I'm not sure what to do in my JSP to actually display my User ID's.
My code:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String dbURL = "jdbc:mysql://localhost:3306/heading360";
String username = "root";
String password = "";
Connection connection = (Connection) DriverManager.getConnection(
dbURL, username, password);
Statement statement = (Statement) connection.createStatement();
ResultSet userId = (ResultSet) statement.executeQuery("SELECT UserId FROM User");
request.setAttribute("userId", userId);
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/testUserId.jsp");
dispatcher.forward(request, response);
catch (SQLException e) {
for (Throwable t : e)
t.printStackTrace();
}
flukyspore wrote:
OK thanks for the quick response.
I am using using JSP EL to write my JSP's. Still learning JSTL.Bad idea. Learn JSTL.
I understand everything you're saying, except I can't seem to find the exact approach to load my ResultSet into an object or collection. Could you give me an example of how to do that. My ResultSet is one column of User ID's, they are integers. Something like this:
private static final String FIND_USER_ID_SQL = "SELECT USER_ID FROM USERS";
private Connection connection;
public List<Integer> findUserIds()
List<Integet> userIds = new ArrayList<Integer>();
Statement stmt = null;
ResultSet rs = null;
try
stmt = connection.createStatement(sql);
rs = stmt.executeQuery();
while (rs.next())
userIds.add(rs.getInt("USER_ID"));
catch (SQLException e)
e.printStackTrace();
finally
close(rs);
close(stmt);
return userIds;
}Fill in the blanks.
%
Similar Messages
-
How to display a pdf file in jsp
hi,
How to display a pdf file in jsp iam having a class which will return fileinputstream of the file object. the pdf file is in server.
regards
ArulA JSP is a combo of HTML and Java, so you can't really "display" a PDF file in a JSP.
You can provider a href link to the PDF file in your JSP.
You can use some utility package to read the contents of the PDF, pull certain things out of it, and display that in your JSP as html
In a servlet you can set the content type to application/pdf and write the binary data of the PDF back to the browser. Once the browser finishes reading in the data it should open the PDF. -
How to display rows of data in JSP?
Hi,
I am learning Servlet and JSP. There is an exercise which requires us to access a database from a Servlet and display the retrieved data in a JSP.
Now I am ok with the database connection. So how to display the data using some kind of loop?
RegardsThey're called ResultSets and they are returned when you execute a query from a Statement. The next() method will tell you whether or not you have another row of data to read.
-
How to display URL of Trex on JSP
Hi, Master:
I am working on B2B Java part. on JSP:
I need display where trex server located (URL) ie: http://lpaux001:50000, can I and how?
Cheers,
EricDuplicate of thread Re: in B2B java, how can I find server url in JSP?
Easwar Ram
http://www.parxlns.com -
How to display record in hyperlink in JSP?
Does any knows how to display the records in hyperlink? That's mean when I retrieve data from one database....two records...let's say peter and thomas....then i want the two records display in hyperlink....that's mean two records can be clicked by the user....and after the user clicked on either one of the record.....the record's value will be able to pass to the other page for use...
Can anyone pls help me of this?Thank in advanced....This will pass the Value from the DB as Query Parameters to the Target JSP.
Peter
Thomas -
How to display List of objects in jsp?
Hi,
I have a list containing Collection of bean objects.
Now I want to display the values in the list of objects in a jsp using JSTL.
Any body help me how to retrieve the bean objects from the List and how can i display that values in the jsp.
Advanced Thanks,
MahendraHave you tried to use <:forEach></c:forEach> tag available in jstl?
-
JSP: How do display list of objects in jsp table?
sorry that my question my confuse you, let me explain more.
in my java class, have method : public List retrieveAllVacancies(); and it returns a list of available vacancies.
on my jsp page, i want to display the vicancies in the table have 3 rows:
vacancy title | location | contract type
should i use some form of for loop or iterator to get the vacancies in the list? how does the jsp page get the list of objects?
anyone can help me? thanksi found out the actual thing i look for is how to pass the vector to the jsp page.
<table>
<%
Vacancy vacancy= new Vacancy();
Vector allVacancies = new Vector();
*//allVacancies = (java.util.Vector)request.getAttribute("allVacancies");*
if(allVacancies.size() == 0)
out.print("<br><br><br> There are no vacancies available!!");
else
%>
<tr>
<td>Vacancy Title</td>
<td>Contract Type</td>
<td>Location</td>
</tr>
<%
for (int i=0;i<allVacancies.size();i++)
vacancy = (Vacancy)allVacancies.get(i);
%>
<tr>
<td><% out.print(vacancy.getVacancyTitle());%></td>
<td><% out.print(vacancy.getContractType());%></td>
<td><% out.print(vacancy.getVacancyLocation()); %></td>
</tr>
<%
%>
</table>*//allVacancies = (java.util.Vector)request.getAttribute("allVacancies");*
if i have this line, when i open the jsp page it will gives NullPoniterException.
the following was what i found for the similar problem,
You can add the Vector to the HttpSession with_
session.setAttribute(String name, Object value)_
or to the ServletRequest with_
request.setAttribute(String name, Object value)._
In the other JSP, retrieve the value with_
session.getAttribute(String name)_
or to the ServletRequest with_
request.getAttribute(String name)._
but i tried to do in this way it didn't work. for sure i didn't do it correctly. could anybody give bit more explanation about how to pass the Vector to the jsp page?
any help would be appreciated. -
How to display XML content in a JSP
Hi,
can anyone help me in displaying xml content in a JSP?I think you want to display value from XML page to the
JSP.If thats the case you can try out this
xml...
<component-profile>
<property name="parm" value="Hi"/>
</component-profile>
jsp...
<% var=profile.getProperty("parm");%>
Hope this helps
gEorgE -
Hi all.... how to display tables dynamically in a JSP page
im trying to display the list of tables in MYSQL datbase into a JSP.... i have a database name link on the page.... once i click on that the list of tables should be listed dynamically in the page as a link...Also is it possible to click on each table link and go to see the table details ...if so how.... please some one help me ...its very urgent........
im trying to display the list of tables in MYSQL
datbase into a JSP.... i have a database name link on
the page.... once i click on that the list of tables
should be listed dynamically in the page as a
link...Also is it possible to click on each table
link and go to see the table details ...if so how....DatabaseMetaData has a method (look in the documentation) to get the list of tables. It also has methods to get information on specific database objects
please some one help me ...its very urgent........It's even urgent that you look in the documentation. -
How to display a document content in JSP page
Hi friends,
I am trying to display a document's content in a JSP page after user authentication.For that I mapped a jsp file with extension .sens in Content Management SDK manager,and put it the file in the directory '/ifs/jsp-bin' of webstarterapp,but it is not working.Down I am giving the code snippet,please help me.I need this to be done fast.
<HTML>
<HEAD>
<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="oracle.j2ee.connector.Login" %>
<%@ page import="oracle.ifs.adk.filesystem.IfsFileSystem"%>
<%@ page import="oracle.ifs.adk.http.HttpUtils"%>
<%@ page import="oracle.ifs.beans.LibrarySession"%>
<%@ page import="oracle.ifs.beans.DirectoryUser"%>
<%@ page import="oracle.ifs.beans.PublicObject"%>
<%@ page import="oracle.ifs.beans.Document"%>
<%@ page import="java.io.*"%>
<jsp:useBean id="login" scope="page" class="oracle.j2ee.connector.Login">
<TITLE>FileExtension.jsp</TITLE>
</HEAD>
<BODY BGCOLOR="SILVER">
<%
String theDisplay = "initialized";
String theHidden = "initialized";
boolean theAdminRights = false;
try
login = (Login) request.getSession(true).getValue("IfsHttpLogin");
LibrarySession ifsSession = login.getSession();
IfsFileSystem ifsFile = new IfsFileSystem(ifsSession);
DirectoryUser du = ifsSession.getDirectoryUser();
String path = HttpUtils.getIfsPathFromJSPRedirect(request) ;
PublicObject po = ifsFile.findPublicObjectByPath(path);
if(po instanceof Document)
Document doc =(Document) po;
InputStream in= ifsFile.getDocumentContent(doc);
int i=in.read();
while(i!=-1)
out.write(i);
in.close();
out.flush();
out.close();
theAdminRights = du.isAdminEnabled();
theDisplay = po.getAttributeByUpperCaseName("DISPLAY").toString();
theHidden = po.getAttributeByUpperCaseName("HIDDEN").toString();
catch (Exception e)
e.printStackTrace();
%>
Here is some static content.
<P>
The Display Value: <%= theDisplay %>
<P>
<%
if (theAdminRights)
%>
The Hidden Value: <%= theHidden %>
<%
%>
</jsp:useBean>
</BODY>
</HTML>these may help you.
Google map integration to EBS -
How to display a html report on jsp page---urgent
i want to display a html report wen a tab is clicked.can any tell me how to do tat.
Didn't quite get your requirement. What do you mean by an HTML report?
-
How to display japanese charecters in the JSP page
Hi,
Below is my jsp page where i am trying to read the japan character from the properties file
through the Resource bundle.
===============================================
<%@page contentType="text/html" %>
<%@page pageEncoding="UTF-8"%>
<%@ page import ="java.util.*" %>
<!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>JSP Page</title>
</head>
<body>
<h1>JSP Page</h1>
<%Locale locale = new Locale(request.getParameter("country"));
ResourceBundle bundle = ResourceBundle.getBundle("com.sunit.regtool.resources.RegTool",locale);
String namelabel =bundle.getString("userName");
String passwordlabel= bundle.getString("password");
%>
<%=namelabel %>
</body>
</html>
=========================================================
In the above code i am getting the value of the country from request object i.e its value
will be en for English ,ja for japanese etc.
I am able to get the value from Resource bundle but when my country is japan
i am not able to see the same japan characters in the browser when i get the JSP on the browser at runtime .
It shows me " �����…� ˆ�ƒ•�‚��ƒ��ƒ��ƒ‰" where the charecters are different.
But when i see in the properties file it is fine.
Please help me what need to be done furher for this.
Thanks,
KiranI think you need to install japanese language fonts in your browser.
-
How to display images [Local Path] of JSP in firefox?
Hi,
I have been facing this problem so long. I have tried every possible option available on the net. like changing '\' to '/' and give complete path like 'file:///D:/images/image.gif' etc. but still facing the same problem.
Its shows the image perfect in IE6 when I pass local drive path but in case of Firefox it never display anything, but when I tried to get Img Src path in alert it gives correct in IE6 and FX1.5 both.
Plz. provide the solution if anybody face the same prob.Create a servlet which listens on /image or so and which sends the image to the HttpServletResponse#getOutputStream().
For example img src="/image?id=123" should invoke the ImageServlet with the request parameter ID = 123. Then locally open 123.jpg and send it to the response.
Also see http://www.google.com/search?q=imageservlet
Don't forget to make it hack-safe. Strip slashes and output images only. -
How do display ResultSet in multiple pages ? Please help !!!
Hi all,
I want to display the ResultSet of my JSP in multiple pages.I want to group them as a set of 10 records on one page and so on the output
displayed on further pages in a set of 10 records per page.
Is there any Simple way to do that ?
Any response will be highly appreciated.
Thanks in advance for any help.
regards,
savdeepHaii
From the result set put your values inside any util objects like vector or hastable and make use of them to show the result in the page..
And again it depends on the requirement..
regards
Shanu -
Display pop ups in the jsp by using Java script
Hi
can any body say ,how to display pop ups in the jsp by using Java script ?that's correct. You can use the below code for AJAX request.
<script type="text/javascript">
var httpObject = getHTTPObject();
//create XMLHttpRequest object
function getHTTPObject() {
var xmlhttp;
if (window.XMLHttpRequest) // if Mozilla, Safari etc
xmlhttp = new XMLHttpRequest();
else if (window.ActiveXObject){ // if IE
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
catch ( e ){
try{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
catch ( e ){}
return xmlhttp;
//define the function to send the request
function sendRequest(){
var currDesc = document.getElementById("description").value;
var URL = "manageMaintAction.do"; //action mapping in your struts-config
var queryString = "currDesc="+escape(currDesc); //get the currDesc value in your action class like request.getParameter("currDesc")
httpObject.open( "Post", URL, true );
httpObject.onreadystatechange = cbFn;
httpObject.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded");
httpObject.send(queryString);
//callback fn
function cbFn() {
if (httpObject.readyState == 4)
if (httpObject.status == 200)
var result = httpObject.responseText;
alert(result);
</script>
Maybe you are looking for
-
Paging problem in jsp with resultset
Could you please take a look at this post. http://forum.java.sun.com/thread.jspa?threadID=632626&tstart=10
-
Define a line layout variant for item display
I am getting the following message "Define a line layout variant for item display" while displaying the document via FPL9. Thanks for the help. Kumar
-
Migrating schemas - Different tablespace names on source and destination
Hi, I am migrating database schemas with exp/imp from 8i on Solaris to 9i on Linux and also from one 9i on Linux to another 9i on another Linux machine. The tablespaces and schemas (empty) already exist in the destination, so the schemas now need to
-
i ve got system with window 7 i got adobe reader 9.0 version ADE 2.0 adobe air Adobe Flah Player 11 active x Adobe shockwave player 11.5 i not able to download the programm with the Adobe ownload assitant of adobe, i didn t understand what i need to
-
How to redirect the clients software updates to the same server but different IP?
Hello Everyone, Well our Mac expert left the company and I got promoted to take his place. I only have about 7 months experience in the Mac world and I have about 90 Macs in the company. Anyways, our company moved into a new building and the Mac serv