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