Solved

Question in JDBC

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 82
splitOdd10 challenge 5 104
advertisement module in core php 4 159
how do i compare an object based on two fields 6 53
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
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…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

786 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