Solved

using prepared statement

Posted on 2000-05-12
1
255 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 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

730 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