Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2003-10-21
7
Medium Priority
?
248 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

WooCommerce is becoming the most powerful e-commerce plugin for Wordpress. And why not. The platform comprises of numerous core plugins that may come in handy, powerful options to make your website development task much easier.
Geo-targeting is the practice of distributing content based on a person’s location, as best as you can determine it. Let’s look at some ways you could successfully use this tactic. The following tips and case studies could lead to meaningful results.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…

610 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