?
Solved

using prepared statement

Posted on 2000-05-12
1
Medium Priority
?
257 Views
Last Modified: 2008-02-26
conside the following  query which is executed using Stament stmt..
sqlcom = "select count(*) from bloodbanks where ucase(city) = \'" + city.toUpperCase() + "\'";

if (!pin.equals(""))
      sqlcom = sqlcom + " and pin = \'" + pin + "\'";

if (!area.equals(""))
      sqlcom = sqlcom + " and ( ucase(area) like  \'" + area.toUpperCase()  "%\')";  
   
Statement stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sqlcom);

now how to execute the above using prepared statement.
0
Comment
Question by:qader99
[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
1 Comment
 
LVL 1

Accepted Solution

by:
mohans earned 150 total points
ID: 2804166
String sqlcom = "select count(*) from bloodbanks where ucase(city) =?";
Vector params = new Vector();
params.addElement(city.toUpperCase());

if (!pin.equals("")) {
  sqlcom = sqlcom + " and pin = ?";
  params.addElement(pin);
}

if (!area.equals("")) {
  sqlcom = sqlcom + " and ( ucase(area) like ?";
   params.addElement( area.toUpperCase() );
}

PreparedStatement ps = con.prepareStatement(sqlcom);
for (int i=0; i<params.size(); i++){
  ps.setObject(i+1, params.elementAt(i));
}

ResultSet rs = ps.executeQuery();
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month10 days, 18 hours left to enroll

770 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