Solved

Question in JDBC

Posted on 1997-08-29
5
206 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 100 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

691 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