日期:2014-05-16  浏览次数:20520 次

JDBC template常用方法总结
[size=xx-small]前言[/size]

   本来对用法不是很想总结的,因为这些东西都是拿过来用,对原理不理解的我,觉得写这些总结就和网上铺天盖地的文章一样。但是昨天写了一篇发现想用的话其中还是有那么点问题的。所以就把JDBC template的常用总结再写一遍吧。

   好啦,言归正传。

JDBC 配置


jdbc.properties
# Properties file with JDBC and JPA settings.
#
# Applied by <context:property-placeholder location="jdbc.properties"/> from
# various application context XML files (e.g., "applicationContext-*.xml").
# Targeted at system administrators, to avoid touching the context XML files.

#-------------------------------------------------------------------------------
# MySQL Settings

#加载驱动
jdbc.driverClassName=com.mysql.jdbc.Driver 
#指定数据库、设置字符集、编码格式、自动连接
jdbc.url=jdbc:mysql://localhost:8080/?useUnicode=true&characterEncoding=UTF8&characterSetResults=UTF8&autoReconnect=true
#数据库名字
jdbc.username=root
#数据库密码
jdbc.password=123456


#mysqlmysql\u8FDE\u63A5\u5173\u95ED c3p0\u672A\u5173\u95ED\u95EE\u9898
c3p0.minPoolSize = 1  
c3p0.maxPoolSize = 50  
c3p0.initialPoolSize = 1  		
c3p0.maxIdleTime = 25000 
c3p0.acquireIncrement = 1  
c3p0.acquireRetryAttempts = 30  
c3p0.acquireRetryDelay = 1000  
c3p0.testConnectionOnCheckin = false  
#c3p0.automaticTestTable = t_c3p0  
c3p0.idleConnectionTestPeriod = 18000  
c3p0.checkoutTimeout=5000  
    
# Properties that control the population of schema and data for a new data source
#jdbc.initLocation=classpath:db/mysql/initDB.txt
#jdbc.dataLocation=classpath:db/mysql/populateDB.txt

# Property that determines which Hibernate dialect to use
# (only applied with "applicationContext-hibernate.xml")
#hibernate.dialect=org.hibernate.dialect.MySQLDialect

# Property that determines which JPA DatabasePlatform to use with TopLink Essentials
#jpa.databasePlatform=oracle.toplink.essentials.platform.database.MySQL4Platform

# Property that determines which database to use with an AbstractJpaVendorAdapter



解读jdbc.properties
<?xml version="1.0" encoding="UTF-8"?>
<!-- - DispatcherServlet application context for PetClinic's web tier. -->
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:oxm="http://www.springframework.org/schema/oxm"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
				http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
				http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd">

	<!-- c3p0 的数据库连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		destroy-method="close">
		<property name="driverClass" value="${jdbc.driverClassName}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<property name="minPoolSize" value="${c3p0.minPoolSize}"/>
		<property name="maxPoolSize" value="${c3p0.maxPoolSize}"/>
		<property name="initialPoolSize" value="${c3p0.initialPoolSize}"/>
		<property name="maxIdleTime" value="${c3p0.maxIdleTime}"/>
		<property name="acquireIncrement" value="${c3p0.acquireIncrement}"/>
		<property name="acquireRetryAttempts" value="${c3p0.acquireRetryAttempts}"/>
		<property name="acquireRetryDelay" value="${c3p0.acquireRetryDelay}"/>
		<property name="testConnectionOnCheckin"  value="${c3p0.testConnectionOnCheckin}"/>
		<!-- <property name="automaticTestTable" value="${c3p0.automaticTestTable}"/>