Solved

Data Comparison.

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Employees depend heavily on their PCs, and new threats like ransomware make it even more critical to protect their important data.
This article outlines the process to identify and resolve account lockout in an Active Directory environment.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

773 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