Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Data Comparison.

Posted on 2002-06-22
4
Medium Priority
?
224 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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It’s time for spooky stories and consuming way too much sugar, including the many treats we’ve whipped for you in the world of tech. Check it out!
As much as Microsoft wants to kill off PST file support, just as they tried to do with public folders, there are still times when it is useful or downright necessary to export Exchange mailboxes to PST files. Thankfully, it is still possible to e…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

597 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