Solved

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

Posted on 2003-10-21
7
226 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 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:lodelode
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Marketing can be an uncomfortable undertaking, especially if your material is technology based. Luckily, we’ve compiled some simple and (relatively) painless tips to put an end to your trepidation and start your path to success.
HOW TO: Connect to the VMware vSphere Hypervisor 6.5 (ESXi 6.5) using the vSphere (HTML5 Web) Host Client 6.5, and perform a simple configuration task of adding a new VMFS 6 datastore.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now