sring框架的jdbc应用

 
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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd ">
   <!-- Initialization for data source -->
   <bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/test"/>
      <property name="username" value="root"/>
      <property name="password" value="wuxu112411"/>
   </bean>
   <!-- Definition for studentJDBCTemplate bean -->
   <bean id="DaoJdbc"
      class="com.cn.DaoJdbc">
      <property name="dataSource"  ref="dataSource" />   
   </bean>
</beans>
javaDAO层获取以及使用
 private DataSource ds;
 private JdbcTemplate jdbc;
 
 public DataSource getDs() {
  return ds;
 }
 @Override
 public void setDataSource(DataSource ds) {
  this.ds = ds;
  this.jdbc = new JdbcTemplate(ds);
 }
 @Override
 public void create(String name, String password) {
 String sql = "insert into t_user (username, password) values (?, ?)";    
 jdbc.update( sql, name, password);
  System.out.println("Created Record Name = " + name + " Age = " + password);
 }
 
 
posted @ 2019-02-12 08:43  愚蠢的程序员  阅读(203)  评论(0编辑  收藏  举报