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:oracle:thin:@kingkong.mar.bogusbank.com:1530: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