A JSP database operation problems

Category: Web Develop
 
macpin
2011-07-29 12:21:43

Sponsored Links
Database: sqlserver
Requirements: Retrieve a table in a column that has duplicate content, requiring the page displays the contents of the column duplicate content is displayed only once.
my code:
<%
try {
String sql = "select distinct Ptype from Resource";
ResultSet ; rs = smt.executeQuery (sql);
if (rs.next ())
{
out.println ("<h2> has uploaded video Categories </ h2> <br> ");
} / / if
else
{
out.println (" <h3 align=center> There is no show! </a> <br> ");
}
while (rs.next ())
{
String classes = rs.getString (1);
out.println ("<a class = 'link' href = 'classlink.jsp? classname =" + classes + "' > "+ classes +" </a> ");
} / / while
smt.close ();
conn.close ();
} / / try
catch (Exception ex)
{
out.println ("<h1 align=center> database connection error! </ h1>");
}
%>
displayed results are wrong, about all of the content in this column are displayed.
---------------------------------------------- -------------------------------------------------- ------------------------------------------
to thunderous Moderators the solution:
change sql query as String sql = "select Ptype from Resource group by Ptype";
finally got the correct results .
---------------------------------------------- -------------------------------------------------- ------------------------------------------
question now is , how to use the query String sql = "select distinct Ptype from Resource"; also be able to get the correct results yet.
also invited the friends to give answers, thank you.

Sponsored Links

scmyns
2011-07-29 12:24:30
alter sql query as String sql = "select Ptype ; from Resource group by Ptype ";
wangdongyangmama
2011-07-29 12:40:15
group by is grouped. duplicate data it just take time, so the data from sql query is not repeated.
codragon2110
2011-07-29 12:56:31

- shows the results are wrong, about all of the content in this column are shown out.
ResultSet this one result set, plus rs.getString (1) how the column will not have all of the content show up?
huageda123
2011-07-29 01:04:57
select distinct Ptype from Resource

first direct look at the results of this statement executed correctly.
if the normal check again JSP.
francisngl01
2011-07-29 01:24:43
test in ms sql2000 sql statement
select Ptype from Resource group by Ptype
and
select distinct Ptype from Resource
results obtained are no duplicate data
hiwing009
2011-07-29 01:28:43
Upstairs friends did not see what I mean, my question is to use the String ; sql = "select distinct Ptype from Resource";
How can I get the correct results. I used to say way to solve your problem, but why I will not do it previously used?
lxt1123
2011-07-29 01:43:21
Are you running inside look into the query analyzer Oh! ! No problem statement

code I suggested a:
if (rs.next) {
}
while (rs.next ()) ;
{

displays a result you will be less, you should use this
if (rs.next) {
do {
String classes = rs.getString (1);
out.println ( "<a class = 'link' href = 'classlink.jsp? classname =" + classes + " '> "+ classes +" </a> ");
} while (rs.next ());
}
zq3024
2011-07-29 01:54:17
In addition to your code a little problem.
you use the following statement to determine whether there was a result set.
if (rs.next())

In this case, the cursor is already pointing to the next record,
results will show you more than you check out the results of a small one.
replace it with the following in this way.

rs.last();
int rowCount=rs.getRow();
//rowCount
shy2003
2011-07-29 02:11:44
off your broken machine, restart, is not enough, for a JDBC driver! In early 2007 the latest 2005 driver, compatible with 2000
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.