Announcement Announcement Module
Collapse
No announcement yet.
PreparedStatements Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • PreparedStatements

    I am new to Spring and I was looking at the v1.1.2 JdbcTemplate source and it looks like PreparedStatements are closed after every execution and then recreated the next time the statement is going to be executed. Is this correct? If so, can someone explain the motivation behind closing the statement after using it once. I would like to reuse the PreparedStatement for the next set of parameters if possible, is there someway to do this in Spring? Thank you for any help you can provide.

  • #2
    This is appropriate use of the JDBC API. PreparedStatement pooling is the responsibility of the DataSource--for example, that provided by an application server, or Commons DBCP. Please refer to the documentation with your pool or server for how to configure pooling options.

    Comment


    • #3
      Prepared Statement in Spring/ Con.close

      2 part Q: This is extension to the Q: maybe redudant abt Prepared Statement and second one is how to use Prepared Statement in Spring

      i have the following class
      Code:
      public GoGreatYetiJava extends DataFather{
      Connection oraCon = null;
      	PreparedStatement psmt = null;
      	Statement stmt = null;
      	private static final String sql = "INSERT INTO YETI_IN_NEWYORK(JK,LG) VALUES("?,?") ":
      public DataSource getOraDataSource() {
      		return oraDataSource;
      
      // now this dataGot is populated everytime it gets a row from some by database by virtue of extending DataFather
      datagot(map){
      Objec l= map.get("JK");
      Objec n= map.get("LG");
      
      //calling insert
      insert(Objec l,Objec n);
      }  
      
      
      public void insert(Object l, Object n) 
      		 {
      		
      		try {
      
      			
      		oraCon = oraDataSource.getConnection();
      		psmt = oraCon.prepareStatement(sql);
      		
      		psmt.setObject(1,l);
      		psmt.setObject(2,n);
      		psmt.executeUpdate();
      
      		
      
      			
      		} catch (SQLException e) {
      			 
      			e.printStackTrace();
      		}finally{
      			if(psmt != null)
      				try {
      					psmt.close();
      				} catch (SQLException e1) {
      					logger.fatal("Cannot close psmt",e1);
      					e1.printStackTrace();
      				}
      			if(oraCon != null)
      				try {
      					oraCon.close();
      				} catch (SQLException e2) {
      					logger.fatal("Cannot close oraCon",e2);
      					e2.printStackTrace();
      				}
      				logger.info("in insert method INSERTED");	
      		}
      		
      	}
      now each time this dataGot gives me 1 row of map's and each time i call the insert()

      am i doing it right?
      meaning opening the connection, then closing it after psmt.executeUpdate(), do we need to close the conn, i am using DataSource so pool is already available, should i close always or just do it once all the values are inserted, how do i know if there are any values left?

      How do we do it using JdbcTemplate?
      i,e prepared statement
      and i tried reading spring ref docs, what u mean by callback methods?

      here's the xml file for dependency injection
      Code:
      <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
      		<property name="driverClassName"><value>oracle.jdbc.driver.OracleDriver</value></property>
      		<property name="url"><value>jdbc&#58;oracle&#58;thin&#58;@kingkong.mar.bogusbank.com&#58;1530&#58;BIGRATD1</value></property>
      		<property name="username"><value>Poigdev</value></property>
      		<property name="password"><value>notalwayswelcome</value></property>
      	</bean>
      i have a Yeti.xml where i do a DP (setter dependcy injection) of dataSource, this and tat the config's are fine through beanRegistry, so the probelm i'm using half of the technology spring provides, still in the learning curve
      Code:
       bean id="yeti" class="com.rat.cat.xock.GoGreatYetiJava">
      		<property name="oraDataSource"><ref bean="DataSource"/></property>
      pls help, if somebody could show the right code and clarify some concepts

      Comment

      Working...
      X