Solved

using prepared statement

Posted on 2000-05-12
1
252 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
1 Comment
 
LVL 1

Accepted Solution

by:
mohans earned 50 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
GUI builder for Eclipse? 8 35
servlet example 17 31
ejb on wildfly 5 20
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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 about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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 …

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