?
Solved

Question in JDBC

Posted on 1997-08-29
5
Medium Priority
?
208 Views
Last Modified: 2010-04-16
I'm using JDBC to execute "select customer_name from customer where customer_num = ? " in database by calling prepareStatement->setInt(1,n)->executeQuery->getString.
But the result is correct for the first attempt only. When I using a for-loop to change variable n and all of the results will become the same as the first record I retrieved! Please give me some advice on how to use prepareStatement correctly! Thank you for your help!
0
Comment
Question by:tomytsui
[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
  • 3
  • 2
5 Comments
 
LVL 1

Accepted Solution

by:
sunot earned 200 total points
ID: 1226014
I don't really quite see your problem as the steps you took are correct. Maybe you could show a portion of your code.

But in your case, remember that you need 2 loops, one is for the changing value of n and the other for the various records retrieved for that particular n.

What I mean here is if each customer_num has several customer_name, then you'll need these loops.

For example, in a for loop, you could code something like this :

for (.........) {
   prepare.clearParameters();
   prepare.setInt(1,n);
   ResultSet results = prepare.executQuery();
   while (results.netx()) {
      ......... results.getString(1)....;
   }
}

I hope this helps you. A portion of your code could help in determining the cause to that mishap.
0
 

Author Comment

by:tomytsui
ID: 1226015
Is that means I should clearParameter() before setInt again?
0
 
LVL 1

Expert Comment

by:sunot
ID: 1226016
The clearParameter is just there to ensure that all the older parameter values are removed before specifying a new one. This is so that you wouldn't have a concat value of the parameter in use.
0
 

Author Comment

by:tomytsui
ID: 1226017
Thank you very much
0
 

Author Comment

by:tomytsui
ID: 1226018
I have tried your method in prepare query statement, it is not working as before! But I try to use prepareStatement to perform insert or update statements, they work well! So I think there is something wrong in ResultSet returned by executeQuery(), it always store the result of the first attempt!

Please comment on it again!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month9 days, 19 hours left to enroll

762 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