?
Solved

creating a drill down page to display details of a chosen user

Posted on 2003-10-21
7
Medium Priority
?
252 Views
Last Modified: 2011-09-20
Hi everyone,

i have a very urgent application which requires me to create a drill down logic.
To put it in a nutshell, part of my application allows administrators to create accounts for users to access the system.

In order to keep track of the number of users created, there will be a "list all users" page which allows the administrator to have a view of all the users that he/she has created, listed in a table format using the username.

how can i perform my SQL query and how do i code my jsp in such a way that for each user, the id associated with it will be included in the <a href> link so that the userid can be passed to the next page, and this id can be used as part of the sql query to retrieve the details of the selected user.

I am a fresh newbie to jsp but have prior knowledge on sql queries and how to query the database via jsp.

Assuming my database has two attributes: username and userid.

egs would be
                                                       List All Users
                                                         Martin
                                                         Kenneth
                                                         Eugene

martin kenneth and eugene will each have a unique userid, i would like to include this userid inside the a href link when the admin clicks on the name, so that the next page can display details of the chosen user...

Can anyone help me out on this issue? Thanks!
0
Comment
Question by:lodelode
  • 4
  • 2
7 Comments
 
LVL 15

Accepted Solution

by:
jimmack earned 375 total points
ID: 9591728
How about:

<table ...
<th...
<%
ResultSet rs = stmt.executeQuery("SELECT username, userid FROM ...
String uname;
String uid;

while (rs.next())
{
    uname = rs.getString("username");
    uid = rs.getString("userid");
%>
<tr><td><a href="...?uid=<%= uid %>><%= uname %></a></td></tr>
<%
}
%>

</table>
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9591751
Sorry,  hit submit too soon.

Once you've created the table and it's appropriate headers, get your ResultSet and then iterate through it for each user.

In the example, the href element appends the "?uid=" and user id value to the end of your next URL (not known so shown as ...).

The username is encapsulated in the <A HREF> anchor.

Any good?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9596771
In addition to jimmack here is how your code should look like in these two pages..

<%@ page import="java.sql.* "%>
<%
String dbDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
// here dbDriver will be whatever driver you decide to use, I am assuming you are using s jdbc odbc bridge.
try{
Class.forName(dbDriver);
String dbURL = "jdbc:odbc:Test"; // Test will be replaced by your System DSN name that u will create.
Connection dbCon = DriverManager.getConnection(dbURL, "", "");
Statement st = dbCon.createStatement();
ResultSet rs = st.executeQuery("SELECT username,userid FROM mytable");
%>
<TABLE>
<TR>
<TH>User name</TH>
</TR>
<%
while(rs.next())
{%>
<TR><TD><a href="second.jsp?uid=<%= rs.getString(2) %>><%= rs.getString(1) %></a></TD></TR>
<%}
st.close();
dbCon.close();
}catch(Exception e)
{}
%>
</TABLE>

now in your second.jsp

<%
String uid = request.getParameter("uid");
Now from here on you can again connect to database and then can get other data that you want to find.

Hope this helps.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lodelode
ID: 9598287
Hi ,

thanks for the jsp codes, so far it is working but how can i check if my database has values first before i show my output..ie, if no users have been created, is there a way i can count the number of rows in the database and if its 0 then i can say something like "no users created"?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9598324
The line

if (rs.next())

line that I included in my first post will determine if there are any records returned.
0
 

Author Comment

by:lodelode
ID: 9598378
Hi jimmack,

pardon me if i'm wrong but in your coding you use
while(rs.next()); not if(rs.next())

my methodology for creating the check is as follows, not sure if its correct

<%
boolean b = rs.first();
if (b == false){
out.println("no users created");
}
else {
rs.beforeFirst();  // points the cursor back to the first row
while(rs.next())
{
 name = rs.getString("name");
 username = rs.getString("username");
%></td></tr>
<TR><TD><a href="second.jsp?uid=<%= username %>"><%= name %></a></TD></TR>

<%
}}
statement.close();
connection.close();
%>
</TABLE>

i realized that if i use rs.first();
the cursor would be placed on the first set of data already, thats why i placed a beforeFirst() to make sure the cursor points to the first data in the database..

any opinions on this?
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9598438
Hi lodelode,

  You're absolutely right.  My mistake. :-)

  Another option (if you would like to keep track of the number of records displayed) could be:

<%
int count = 0;
while(rs.next())
{
 name = rs.getString("name");
 username = rs.getString("username");
 count++;
%></td></tr>
<TR><TD><a href="second.jsp?uid=<%= username %>"><%= name %></a></TD></TR>

<%
}

if (count == 0)
{
out.println("no users created");
}

This also avoid moving the cursor about (not that this is particularly significant).
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
In the below post we have mentioned the best hosting type for startups. Also, check out some of the superlative web hosting companies that are proposing affordable web hosting solutions to host your startup website.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Screencast - Getting to Know the Pipeline

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question