`
默翁1
  • 浏览: 25225 次
社区版块
存档分类
最新评论

spring JdbcTemplcate

 
阅读更多

原始的没有配置文件的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"));	
			}
		});
	}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics