原始的没有配置文件的jdbc连接方式:
public class Test1 { @Test public void handle() throws SQLException{ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/hibernate"); dataSource.setUsername("root"); dataSource.setPassword("123456"); Connection connection=dataSource.getConnection(); PreparedStatement statement=connection.prepareStatement("select * from mw_person"); ResultSet rs=statement.executeQuery(); while(rs.next()){ System.out.println(rs.getString("name")); } } }
不使用配置文件依然使用jdbcTemplate的形式:
@Test public void handle1() throws SQLException{ DriverManagerDataSource dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/hibernate"); dataSource.setUsername("root"); dataSource.setPassword("123456"); JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource); jdbcTemplate.query("select * from mw_person",new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { System.out.println(rs.getString("name")); } }); }
使用dbcp连接池
<!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/hibernate"></property> <property name="username" value="root"></property> <property name="password" value="123456"></property> <!-- 容器启动时,自动创建连接的数量 --> <property name="initialSize" value="5"></property> <!-- 同一时间可以从连接池分配的最大可以分配多少个对下对象 --> <property name="maxActive" value="10"></property> <!-- 在抛出异常声明之前,连接回收的最大时间 --> <property name="maxWait" value="1"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
<context:component-scan base-package="dao.**"></context:component-scan> <!-- 引入资源文件 --> <context:property-placeholder location="classpath:dao/db.properties"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${driverClassName}"></property> <property name="url" value="${url}"></property> <property name="username" value="${username}"></property> <property name="password" value="${password}"></property> <!-- 容器启动时,自动创建连接的数量 --> <property name="initialSize" value="5"></property> <!-- 同一时间可以从连接池分配的最大可以分配多少个对下对象 --> <property name="maxActive" value="10"></property> <!-- 在抛出异常声明之前,连接回收的最大时间 --> <property name="maxWait" value="1"></property> </bean> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean>
@Autowired private JdbcTemplate jdbcTemplate; @Test public void handle3() throws SQLException{ jdbcTemplate.query("select * from mw_person",new RowCallbackHandler() { @Override public void processRow(ResultSet rs) throws SQLException { System.out.println(rs.getString("name")); } }); }
相关推荐
Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...
Spring boot 是 Spring 的一套快速配置脚手架,可以基于spring boot 快速开发单个微服务,Spring Cloud是一个基于Spring Boot实现的云应用开发工具;Spring boot专注于快速、方便集成的单个个体,Spring Cloud是关注...
Spring源代码解析(一):Spring中的事务处理 Spring源代码解析(二):ioc容器在Web容器中的启动 Spring源代码分析(三):Spring JDBC Spring源代码解析(四):Spring MVC Spring源代码解析(五):Spring AOP获取Proxy ...
Spring3.2 Spring4.3 Spring5.0三个版本的jar包,都是官方下载,亲测可用,放心下载。
spring boot , spring cloud alibaba, spring cloub 版本选型
Classes contained in spring-mock.jar: org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi....
spring3.1官方所有的jar包 org.springframework.aop-3.1.RELEASE.jar org.springframework.asm-3.1.RELEASE.jar org.springframework.aspects-3.1.RELEASE.jar org.springframework.beans-3.1.RELEASE.jar org....
spring 资料spring 资料spring 资料
将spring2的开发包spring.jar替换为附件中压缩文件的所有jar包,既可以将spring2.0的应用升级到spring3.0.5
简单的spring cloud项目,适合新手学习使用
spring配置 spring配置 spring配置 spring配置 spring配置
spring4.x中的jar包下载,spring4.0.6下载,spring最新稳定版jar包下载 http://maven.springframework.org/release/org/springframework/spring/ 这个链接中有各种稳定版的jar包下载 目前官网上大部分都要maven下载
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
Spring技术内幕:深入解析Spring架构与设计原理(第2部分) 《Spring技术内幕:深入解析Spring架构与设计原理》是Spring领域的问鼎之作,由业界拥有10余年开发经验的资深Java专家亲自执笔!Java开发者社区和Spring...
spring 3.2.4 Realease 的所有jar包: spring-context-3.2.4.RELEASE.jar spring-core-3.2.4.RELEASE.jar spring-beans-3.2.4.RELEASE.jar spring-test-3.2.4.RELEASE.jar spring-web-3.2.4.RELEASE.jar spring-aop-...
通过一个使用Spring、Spring Boot和Spring Cloud的小例子来说明如何构建微服务系统。 具体请看附件文件中的:readme.txt 和 [译]Spring构建微服务.png 访问地址:http://localhost:1111/ 运行顺序:...
spring定时器spring定时器spring定时器spring定时器spring定时器
spring4.3.1全套jar下载。 4.3.1/spring-aop-4.3.1.RELEASE.jar 4.3.1/spring-aspects-4.3.1.RELEASE.jar 4.3.1/spring-beans-4.3.1.RELEASE.jar 4.3.1/spring-context-4.3.1.RELEASE.jar 4.3.1/spring-core-4.3.1....
基于SpringCloud的快递驿站系统源码 基于SpringCloud的快递驿站系统源码 基于SpringCloud的快递驿站系统源码 基于SpringCloud的快递驿站系统源码 基于SpringCloud的快递驿站系统源码 基于SpringCloud的...
项目原型:Struts2.3.16 + Spring4.1.1 + Hibernate4.3.6 二、 项目目的: 整合使用最新版本的三大框架(即Struts2、Spring4和Hibernate4),搭建项目架构原型。 项目架构原型:Struts2.3.16 + Spring4.1.1 + ...