Solved

Data Comparison.

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
attribute vs parameter and setter vs add method 17 83
stateless EJB with glassfish 2 71
reflection example 3 92
IE doesn't Invoke servlet in iframe 1 101
We need a new way to communicate time sensitive or critical info.   The best part of my role at xMatters is visiting our clients all over the world to learn about how they operate their businesses, share insights that xMatters has gleaned across…
With the rapid rise in mobile usage, mobile devices are here to stay and have become an integral part of doing business. Here are 9 great apps for your BYOD environment.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

920 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

15 Experts available now in Live!

Get 1:1 Help Now