Even it is very small thing but I searched on net lot with these words on goolge but didnt get solution.
Now I am going to show here how we can use preaparedstatement with spring JdbcOperations.
Create these five file in your Eclipse project.
DaoOperation.java
IDaoOperation.java
App.java
Myconfig.xml
MyProperties.properties
DaoOperation.java
import java.sql.SQLException;
import java.util.Set;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcOperations;
public class DaoOperation implements IDaoOperation{
private JdbcOperations jdbc;
public void saveStudentData() throws SQLException{
String sql = "insert into student (id,name,marks) values (?,?,?)";
Object[] args = {5,"Suman",99};
executeUdpdateSQL(sql,args);
}
public void updateStudentData() throws SQLException{
String sql = "update student set name=?, marks = ? where id = ?";
Object[] args = {"Ayush Suman",999,5};
executeUdpdateSQL(sql,args);
}
public void executeUdpdateSQL(String sql, Object[] args) throws SQLException {
jdbc.update(sql, args);
}
public JdbcOperations getJdbc() {return jdbc;}
public void setJdbc(JdbcOperations jdbc) {this.jdbc = jdbc;}
}
IDaoOperation.java
import java.sql.SQLException;
/**
* @author Binod Suman
*/
public interface IDaoOperation {
public void saveStudentData() throws SQLException;
public void updateStudentData() throws SQLException;
}
App.java
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args) throws Exception {
IDaoOperation daoOperation;
ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("Myconfig.xml");
daoOperation = (IDaoOperation) context.getBean("daoOperation");
daoOperation.saveStudentData();
// daoOperation.updateStudentData();
}
}
Myconfig.xml
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:p="http://www.springframework.org/schema/p"       
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:lang="http://www.springframework.org/schema/lang"       
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                           http://www.springframework.org/schema/tx 
                           http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
                           http://www.springframework.org/schema/lang 
                           http://www.springframework.org/schema/lang/spring-lang-2.5.xsd
                           http://www.springframework.org/schema/context 
                           http://www.springframework.org/schema/context/spring-context-2.5.xsd
                           http://www.springframework.org/schema/aop 
                           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">                           
 <context:mbean-server/>
 <context:property-placeholder location="classpath:/MyProperties.properties"/>
  <bean id="daoOperation" class="DaoOperation"
 p:jdbc-ref="jdbc" />
 <bean id="jdbc" class="org.springframework.jdbc.core.JdbcTemplate"
  p:dataSource-ref="dataSource" />
  
 <bean id="dataSource" destroy-method="close"
  class="org.apache.commons.dbcp.BasicDataSource" p:driverClassName="${db.driver}"
  p:url="${db.url}" p:username="${db.username}" p:password="${db.password}" />
   
</beans>
MyProperties.properties
db.driver = com.mysql.jdbc.Driver
db.url = jdbc:mysql://localhost/sumandb
db.username = root
db.password = mysql
Tuesday, June 8, 2010
Subscribe to:
Post Comments (Atom)
 
great job i really like your post.keep sharing
ReplyDeletewhere is a preparedstatement? wtf?
ReplyDeletelistening to the sound: the material is the worst Bathtub faucet resolved, good leader is the whole cast copper, Waterfall faucet . If the sound is very crisp, Faucet parts must be stainless steel, the quality difference would a grade
ReplyDelete