Solved

Data Comparison.

Posted on 2002-06-22
4
200 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
  • 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

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.

Join & Write a Comment

This article will show you how to create an ISO CD-ROM/DVD-ROM image (*.iso), and MD5 checksum signature, for use with VMware vSphere Hypervisor 6.5 (ESXi 6.5). It's a good idea to compare checksums, because many installations fail because of a corr…
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
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.

708 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

12 Experts available now in Live!

Get 1:1 Help Now