spring来了-05-JDBC
Posted 起今知行 | 思考者 | Freedom
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring来了-05-JDBC相关的知识,希望对你有一定的参考价值。
概述
- Spring对C3P0连接池的支持很完善
- Spring对jdbc提供了JdbcTemplate,来简化jdbc操作, JdbcTemplate模板工具类,类似于DbUtils组件
JDBC:
1 package cn.fuyi.h_jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 7 public class UserDao { 8 9 public void save() { 10 try { 11 String sql = "insert into dept(dname) values(?)"; 12 Class.forName("com.mysql.jdbc.Driver"); 13 Connection connection = DriverManager 14 .getConnection("jdbc:mysql:///mydb", "root", "950613"); 15 PreparedStatement pstmt = connection.prepareStatement(sql); 16 pstmt.setString(1, "test"); 17 pstmt.execute(); 18 19 pstmt.close(); 20 connection.close(); 21 } catch (Exception e) { 22 e.printStackTrace(); 23 } 24 } 25 }
使用DataSource
1 package cn.fuyi.h_jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 7 import javax.sql.DataSource; 8 9 public class UserDao { 10 11 private DataSource dataSource; 12 public void setDataSource(DataSource dataSource) { 13 this.dataSource = dataSource; 14 } 15 16 public void save() { 17 try { 18 String sql = "insert into dept(dname) values(?)"; 19 Class.forName("com.mysql.jdbc.Driver"); 20 Connection connection = dataSource.getConnection(); 21 PreparedStatement pstmt = connection.prepareStatement(sql); 22 pstmt.setString(1, "test1111"); 23 pstmt.execute(); 24 25 pstmt.close(); 26 connection.close(); 27 } catch (Exception e) { 28 e.printStackTrace(); 29 } 30 } 31 }
1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 2 <property name="jdbcUrl" value="jdbc:mysql:///mydb"></property> 3 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 4 <property name="user" value="root"></property> 5 <property name="password" value="950613"></property> 6 <property name="initialPoolSize" value="5"></property> 7 <property name="maxPoolSize" value="10"></property> 8 </bean> 9 10 <bean id="userDao" class="cn.fuyi.h_jdbc.UserDao"> 11 <property name="dataSource" ref="dataSource"></property> 12 </bean>
使用JdbcTemplate:
1)引入jar文件
spring-jdbc-3.2.5.RELEASE.jar jdbc支持包
spring-tx-3.2.5.RELEASE.jar 事务包
c3p0-0.9.1.2.jar c3p0
mysql-connector-java-5.1.12-bin.jar 驱动类
1 package cn.fuyi.h_jdbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.PreparedStatement; 6 7 import javax.sql.DataSource; 8 9 import org.springframework.jdbc.core.JdbcTemplate; 10 11 public class UserDao { 12 13 private DataSource dataSource; 14 public void setDataSource(DataSource dataSource) { 15 this.dataSource = dataSource; 16 } 17 18 public void save1() { 19 String sql = "insert into dept(dname) values(‘test123‘)"; 20 JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); 21 jdbcTemplate.execute(sql); 22 } 23 }
1 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 2 <property name="jdbcUrl" value="jdbc:mysql:///mydb"></property> 3 <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 4 <property name="user" value="root"></property> 5 <property name="password" value="950613"></property> 6 <property name="initialPoolSize" value="5"></property> 7 <property name="maxPoolSize" value="10"></property> 8 </bean> 9 10 <bean id="userDao" class="cn.fuyi.h_jdbc.UserDao"> 11 <property name="dataSource" ref="dataSource"></property> 12 </bean>
以上是关于spring来了-05-JDBC的主要内容,如果未能解决你的问题,请参考以下文章
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
初识Spring源码 -- doResolveDependency | findAutowireCandidates | @Order@Priority调用排序 | @Autowired注入(代码片段
Spring boot:thymeleaf 没有正确渲染片段
What's the difference between @Component, @Repository & @Service annotations in Spring?(代码片段
spring练习,在Eclipse搭建的Spring开发环境中,使用set注入方式,实现对象的依赖关系,通过ClassPathXmlApplicationContext实体类获取Bean对象(代码片段