Solved

Question in JDBC

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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…

707 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

15 Experts available now in Live!

Get 1:1 Help Now