Solved

Question in JDBC

Posted on 1997-08-29
5
183 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
  • 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

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

867 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

22 Experts available now in Live!

Get 1:1 Help Now