Solved

Data Comparison.

Posted on 2002-06-22
4
215 Views
Last Modified: 2010-08-05
Hi, I need to perform some checking for my data I key in either it's in the database or not.

I used ResultSet, the wasNull() to do the task. But seems it's not the proper way.

Below is my code, any recommandation?

******************** action2.jsp **********************

<%@ page language="java" import ="java.sql.*, java.io.*, java.util.*, com.wrox.cars.*"  %>

<jsp:useBean id="bank" class="com.wrox.cars.japf12" />

<html>
  <head>
      <title>Control </title>
  </head>
<body>

<table border ="2">
  <tr>
     <td> Customer Code </td>
     <td> Security Type </td>
  </tr>

 <%
  String cust = request.getParameter("Customer");
   bank.connect();
   ResultSet rs = bank.data(cust);
   while (rs.next()) {
 %>

  <tr>
     <td align="center"><%= rs.getString("TA1001") %>  </td>
     <td align="center"><%= rs.getString("TA1022") %>  </td>
  </tr>

<!-- I need to perform the checking either the "Customer" which user is in my databse or not. If it's return nothing, I want to print something.. like the code below...... But it's give me false or true only.....   -->

 <tr>
       <td align="center"> <%= rs.wasNull() %>

 <%
     if (rs.wasNull()) {
        out.println("No Data");
     }
    } %>
  </td>

</table>
<% bank.disconnect(); %>
<% rs.close(); %>
</form>
</body>
</html>
0
Comment
Question by:foongkim
[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
  • 2
4 Comments
 
LVL 19

Accepted Solution

by:
cheekycj earned 50 total points
ID: 7100784
a few ways of doing this:

1.  If your JDBC driver is JDBC 2.0 compliant you can do the following:
  int numOfRecords = 0;
  if (rs != null) {
     rs.last(); // move cursor to last record
     numOfRecords = rs.getRow(); // get the total num of records thru current row which is the last row now.
  }
  // now numOfRecords should have number of records returned

  Now if you need to go through the data reset the cursor using row.first();


2.  use a counter:
  int numOfRecords = 0;
  while (rs.next()) {
     numOfRecords++;
  }
  // now numOfRecords should have number of records returned

3.  Modify your query to do a count like 'count(*) as recordcount from yourtable where x = y'
  int numOfRecords = 0;
  while (rs.next()) {
     numOfRecords = rs.getInt("recordcount");
  }
  // now numOfRecords should have number of records returned

One of these should do the job for you.

HTH,
CJ
0
 
LVL 92

Expert Comment

by:objects
ID: 7101153
boolean exists = false;  // true if a customer exists.
ResultSet rs = bank.data(cust);
while (rs.next()) {
   exists = true;
%>

0
 

Author Comment

by:foongkim
ID: 7102374
Thank you. I thought the "rs" can not work like (! rs).

TQ.

I think I am still lack of experience in JSP programming. Any suggestion in the internet where I can download some sample application.
0
 
LVL 19

Expert Comment

by:cheekycj
ID: 7102389
for JSP info check out:
http://www.jspin.com/
But remember JSPs are coded in Java so learning Java is the key here:
http://developer.java.sun.com/developer/infodocs/#tutorials

should get you started on the right foot.

Glad I could help and Thanx for the "A"

CJ
0

Featured Post

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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