Solved

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

Posted on 2003-10-21
7
232 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
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
tomcat users xml 7 122
Limiting the number of characters a user can type in a text field 6 56
main method of string class 3 113
Use tracing facilities in your browser steps 2 120
Google always has something new and amazing up its sleeve, and the most current thing that they have been working on is another step in the evolution of Google Search, from machine learning to its brilliant successor, deep learning.
The next five years are sure to bring developments that are just astonishing, and we will continue to try to find the balance between connectivity and security. Here are five major technological developments from the last five years and some predict…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 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