If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > JAVA > code difference for PreparedStatement and Statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-16-09, 15:43
lmei007 lmei007 is offline
Registered User
 
Join Date: Jun 2008
Posts: 20
code difference for PreparedStatement and Statement

We know preparedStatement in JDBC has performance benefit for frequently called queries because preparedStatement will be compiled and cached on the server.

My question is do we need to avoid multiple calls to create a preparedStatement or it will not matter because server can handle it by check the existing compiled result?

For example, I have a addNewCustomer() method which will be called each time a new customer is added.

1. Do I need to include the preparedStatement creation statements (the first two) inside this method or

2. I should put the first two statements in another method which only be called once at initiaizing stage?

option 2 make more sense to me but my concern is the Connection. If I have a connetion pool and the PreparedStatement is bound with the connection instance, how can I guarante the connection? or I shouldn't care of it at all.

Any expert can explain this to me? thanks,

public int addNewCustomer(Customer customer){

try {
String sql = createPrepStmntStrInsertIntoCustomer();
prepStmntInsertCustomer = m_conn.prepareStatement(sql);

fillPrepStmntInsertIntoCustomer(prepStmntInsertCus tomer, customer);
prepStmntInsertCustomer.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
}

return custId;
}
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On