日期:2014-05-16 浏览次数:20359 次
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <!--PropertyPlaceholderConfigurer类来读取xxx.properties配置文件信息,以key和value的形式--> <property name="locations"> <list> <value> /WEB-INF/classes/config/pkm/environment/jdbc.properties </value> <value> <!--多个xxx.properties文件--> </value> </list> </property> </bean> <!--事实上是因为DriverManagerDataSource建立连接是只要有连接就新建一个connection,根本没有连接池的作用--> <!--两种不同的DataSource--> <!--单纯的DataSource--> <bean id="pkmDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"> <value>${pkm.jdbc.driverClassName}</value> <!--${pkm.jdbc.driverClassName}是jdbc.properties文件 中的key--> </property> <property name="url"> <value>${pkm.jdbc.url}</value> </property> <property name="username"> <value>${pkm.jdbc.username}</value> </property> <property name="password"> <value>${pkm.jdbc.password}</value> </property> </bean> <!--连接池--> <bean id="pkmDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" lazy-init="false"> <property name="driverClassName" value="${pkm.jdbc.driverClassName}"/> <property name="url" value="${pkm.jdbc.url}"/> <property name="username" value="${pkm.jdbc.username}"/> <property name="password" value="${pkm.jdbc.password}"/> <property name="initialSize" value="5"/> <property name="maxActive" value="10"/> <property name="maxWait" value="60000"/> <property name="poolPreparedStatements" value="true"/> </bean>
public class JDBCTest { public static void main(String[] args) { String driver = "com.mysql.jdbc.Driver"; String dbName = "spring"; String passwrod = "root"; String userName = "root"; String url = "jdbc:mysql://localhost:3308/" + dbName; String sql = "select * from users"; try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, userName, passwrod); PreparedStatement ps = conn.prepareStatement(sql); ResultSet rs = ps.executeQuery(); while (rs.next()) { System.out.println("id : " + rs.getInt(1) + " name : " + rs.getString(2) + " password : " + rs.getString(3)); } // 关闭记录集 if (rs != null) { try { rs.close(); } catch (SQLException e) {