More than 1 preparedStatement object

More than 1 preparedStatement object

View Answers

June 2, 2009 at 12:10 PM

Hi Friend,

You can use more than one prepared Statement object in the java class.Try the following code:

import java.sql.*;

public class JdbcPreparedstatementAddbatch {

public static void main(String args[]) {

Connection con = null;
PreparedStatement pst1 = null;
PreparedStatement pst2 = null;

String url = "jdbc:mysql://localhost:3306/";;
String db = "mysql";
String driver = "com.mysql.jdbc.Driver";
String user = "root";
String pass = "root";

try {

Class.forName(driver);
con = DriverManager.getConnection(url + db, user, pass);
pst1 = con.prepareStatement("update data set address=? where name =?");


pst1.setString(1, "Delhi");
pst1.setString(2, "angelina1");
pst1.addBatch();

pst1.setString(1, "Mumbai");
pst1.setString(2, "angelina2");
pst1.addBatch();

pst1.executeBatch();
pst1.close();
pst2 = con.prepareStatement("update data set address=? where name =?");


pst2.setString(1, "Agra");
pst2.setString(2, "AAAAA");
pst2.addBatch();

pst2.setString(1, "Chennai");
pst2.setString(2, "BBBBB");
pst2.addBatch();

pst2.executeBatch();
pst2.close();
con.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}
Here we have taken two fields name and address of varchar type in the database table. We have used following methods.

1) set String ( ) - This method is used to set the value as string in place of question mark place holder.
2) addBatch ( ) - This method is used to add a set of parameter to the batch of command to this sql server prepared object.

3) execute Batch ( ) - This method is used to submit a set of command in sql query to the database, In case all the commands successfully, return you an array update count.

Thanks









Related Tutorials/Questions & Answers:
More than 1 preparedStatement object - Java Beginners
More than 1 preparedStatement object  Hey but I want to use more than one prepared Statement object using batch update.. Explain with a code using java... Thanks  Hi Friend, You can use more than one prepared
How to write more than 65536 rows in single excel file but more than 1 sheets?
How to write more than 65536 rows in single excel file but more than 1 sheets... but more than 1 sheets.. now i can write only less than 65536 rows.. but in my office db having more than 1 crore data.. pls help me... here is my code.. public class
Advertisements
Is Python more powerful than C++?
Is Python more powerful than C++?  Hi, I am beginner in Data Science...: Is Python more powerful than C++? Try to provide me good examples or tutorials links so that I can learn the topic "Is Python more powerful than C++?"
PreparedStatement
PreparedStatement  What is PreparedStatement? Give me example
More than one Faces Configuration file
More than one Faces Configuration file  Is it possible to have more than one Faces Configuration file
more than one struts-config.xml file
more than one struts-config.xml file  Can we have more than one struts-config.xml file for a single Struts application
Apply more than one xsl for single XML
Apply more than one xsl for single XML  How to apply more than one xsl for same xml file
Can a Class extend more than one Class?
Can a Class extend more than one Class?   Hi, Can a Class extend more than one Class? thanks
for writting java program why we are taking more than one class
for writting java program why we are taking more than one class  for writting java program only one class is enough but why we are taking more than one class
Implementing more than one Job Details and Triggers
Implementing more than one Job Details and Triggers... will learn how to implement more than one triggers and jobs with a quartz... of more than one job details and triggers. Description of program:ADS
problem in sending more than 500 values to a jsp page
problem in sending more than 500 values to a jsp page  when i am trying to send more than 500 values from a html form to a jsp page browser is showing that server is not sending any data...I have configured tomcat5.5
Replication factor: 1 larger than available brokers: 0
Replication factor: 1 larger than available brokers: 0  Hi, I am...: 1 larger than available brokers: 0. [2017-12-09 14:25:42,460] ERROR... --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test
mapping between java class and more than 2 tables
mapping between java class and more than 2 tables  Association mapping of table in hibernate   Hi Friend, Please visit the following link:ADS_TO_REPLACE_1 Hibernate Tutorial Thanks
How to get more than one value from ajax
How to get more than one value from ajax  I have multiple select list box in php. i filled the value using ajax. how to get different value to fill the same list box Ex. option value as id (adsbygoogle
Deployment on Server that can be used simultaneously by more than one user
Deployment on Server that can be used simultaneously by more than one user  Sir, I have deployed my web application developed using JSP & sevlet. how could i access it on the network using xp os. I have deployed it on Tomcat
how to handle action events in case of more than one JFrame
how to handle action events in case of more than one JFrame   I have... contains three java files namely 1. Engine.java 2. Snake.java 3. Gameoard.java when i... 1. PlayGame.java RulesDialog.java PlayGame.java has three JButtons :- Play
How to Display Duplicate elements from more than one List in java?
How to Display Duplicate elements from more than one List in java?  How to Display Duplicate elements from more than one List in java? //I mean common elements from two List? Ex: List a1=new ArrayList(); a1.add(50
Writing more than one cards in a WML deck.
   Writing more than one cards in a deck.In this lesson
Hi.. how to write more than one sheets in a excel file... pls anybody help me....
Hi.. how to write more than one sheets in a excel file... pls anybody help me... more than one sheets.. For example: first sheet have complete mean automatically... "+tableName+" having more than 65536 rows"); System.out.println
To get the value of more than one text box in an HTML page to a jsp page - JSP-Interview Questions
To get the value of more than one text box in an HTML page to a jsp page  An html file has a text box as To get the value of this text box in a JSP... will have a name-value pair attribute. JSP has a built in object request
can i restrict a class that it will not allows create more than two objects - Java Interview Questions
can i restrict a class that it will not allows create more than two objects  hi all, can i restrict a class that it will not allows create more than two objects can any help me please. Thaks bala k
Server side validation on dynamically generated fields from more than one table on spring framework.
Server side validation on dynamically generated fields from more than one table on spring framework.   Server side validation on dynamically generated fields from more than one table in spring mvc framework
J query event for selecting the more than one div in tablet browser(Select and drag)?
J query event for selecting the more than one div in tablet browser(Select and drag)?  Web application five div is created. For selecting the more... to select the more than one tag using the touch and drag event
how to restrict user against marking same event more than one times - JSP-Servlet
how to restrict user against marking same event more than one times  Hi I am developing student feedback system for educational institution. Main purpose about this application is to evaluate performance of faculty. Each
Use of "|" separator for selecting more than one path
Use of  "|" separator for selecting more than one path.... It allows you more flexibility by allowing you to execute multiple expressions...;?> <information>   <person id="1
PreparedStatement using batch update - Java Beginners
PreparedStatement using batch update  How to execute different preparedStatement object using batch update(Java) Pls explain with the code... object.i want to use more than one..can i do
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)
Error in connecting to the mySQL database in TOMCAT using more than one PC (database connection pooling)  how do i implement connection pooling..., and I can work with two or more PCs, however this will not be efficient
how can retrieve more than one values in text field using ajax?
how can retrieve more than one values in text field using ajax?  im using php and ajax. Problem is that i want to search seat numbers bases of batch and semester condition on onchange event eg. if i search batch 2008 and semester
select with preparedstatement in java
select with preparedstatement in java  How to write code example for select with preparedstatement in java? Thanks   Hi, Following code examples can be used: PreparedStatement pstmt = conn.prepareStatement
why java ignore backspace event when there are more then 1 keychar for a single keystroke.
why java ignore backspace event when there are more then 1 keychar for a single keystroke.  why java ignore backspace event when there are more then 1 keychar for a single keystroke. Assume that i have a text field to get
Prepared Statement Set Object
should use PreparedStatement object as it reduces the execution time. PreparedStatement object is faster than the Statement object... the PreparedStatement object for sending the parameterized SQL statement to the database
object
object  is it possible to create object in the same class..?.   Yes, you can. class CreateObject { CreateObject(){ System.out.println("I have an object."); } public static void main(String[] args
PHP Date add 1 year
Adding 1 year to a php date object is sometimes useful to solve some... to add 1 year easily. The code given below will show how you can add 1 year in php program. <?php //Example to add 1 year to a date objectADS
Inserting a row using PreparedStatement
INSERING A ROW USING PREPAREDSTATEMENT If you want to execute a statement many time , "PreparedStatement " reduces execution time. Unlike a "Statement" object ,it is given an 'Sql' statement when
Insert an object with the add(Object) method
the usage of the add(Object) method in more detail. Create a class "... Insert an object with the add(Object) method       In this section you will learn to insert an object
Request Object In JSP
Request Object In JSP       This section illustrates more about the JSP implicit object called... illustrates more about the request object for parsing form data by using
Session Object
be saved in a Session Object ?   There is no such limit on the amount of information that can be saved in a Session Object. The only limit is the Session ID length , which should not exceed more than 4K
More About the CronTrigger
More About the CronTrigger       The CronTriggers are more useful than the SimpleTrigger, if we... but here, we should more careful about how many day are used in a month. Months
Are containers faster than VMs
Are containers faster than VMs  Hi, I am trying to compare Containers with the VMs and looking for more information on this. On the performance side which is Good? Are containers faster than VMs? Thanks
Retrieving Data from the table using PreparedStatement
the PreparedStatement object and takes a query as its parameter. In this query we... by using the executeQuery() method of the PreparedStatement object. Now the data... Retrieving Data from the table using PreparedStatement
JavaScript Array undefined
to print an array element more than the declared size of element that an array object hold in it. The Tutorial depicts an example from JavaScript Array Undefined... length. Incase the array length  is more than the declared variable
Understanding Connection Object
. An application may have one or more than one connections with a single database... the Connection object for the following things:ADS_TO_REPLACE_1 1). It creates... Understanding Connection Object   
object of object class
object of object class  what do u mean by "object of object class
Is Java a pure object oriented language?
through the class and object creation. For more object oriented programming language...Is Java a pure object oriented language?   Hi, Is Java a pure object oriented language? thanks   Hi No, Java is an object oriented
Replace an object with set(int, Object) method
that provides the usage of the set(int, object) method in more detail.  Create... Replace an object with set(int, Object) method       In this section you will learn to replace an object
Is DataCamp better than coursera?
Is DataCamp better than coursera?  Hi, I am beginner in Data Science...: Is DataCamp better than coursera? Try to provide me good examples or tutorials links so that I can learn the topic "Is DataCamp better than coursera?"
Is DataCamp better than codecademy?
Is DataCamp better than codecademy?  Hi, I am beginner in Data...: Is DataCamp better than codecademy? Try to provide me good examples or tutorials links so that I can learn the topic "Is DataCamp better than codecademy
Is R better than Python?
Is R better than Python?  Hi, I am beginner in Data Science... than Python? Try to provide me good examples or tutorials links so that I can learn the topic "Is R better than Python?". Also tell me which
Is R faster than Matlab?
Is R faster than Matlab?  Hi, I am beginner in Data Science... than Matlab? Try to provide me good examples or tutorials links so that I can learn the topic "Is R faster than Matlab?". Also tell me which
Is Google bigger than Microsoft?
Is Google bigger than Microsoft?  Hi, I am beginner in Data Science...: Is Google bigger than Microsoft? Try to provide me good examples or tutorials links so that I can learn the topic "Is Google bigger than Microsoft?"

Ads