1.传统方式创建一个数据库连接池
package com.hao.test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
public class DbConnectConfig {
public static final String URL = "jdbc:mysql://192.168.1.160:3306/hhisp";
public static final String USER = "root";
public static final String PASSWORD = "123456";
public static void main(String[] args) throws Exception {
//1.加载驱动程序
Class.forName("com.mysql.jdbc.Driver");
//2. 获得数据库连接
Connection conn = DriverManager.getConnection(URL, USER, PASSWORD);
//3.操作数据库,实现增删改查
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT name_, id_ FROM t_comm_user where id_=3");
//如果有数据,rs.next()返回true
while(rs.next()){
System.out.println(rs.getString("name_")+" 用户id:"+rs.getInt("id_"));
}
}
}
传统方式创建一个数据库连接,每次我们要连接数据库都需要我们获取一个conn对象,这样不仅麻烦,而且会创建多个不必要的数据库连接对象,增加系统负担。
2.使用spring控制数据库连接池
我们可以利用spring的IOC容器中bean对象的单例特性,来控制数据库连接对象,这样我们只需要创建一次数据库连接对象就可以了
1.导入数据库连接jar包c3p0和mysql-connector-java
2.在xml中进行配置
<bean id="db" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="root">
</property>
<property name="password" value="123456">
</property>
<property name="jdbcUrl"
value="jdbc:mysql://192.168.1.160:3306/hhisp">
</property>
<property name="driverClass"
value="com.mysql.jdbc.Driver"></property>
</bean>
3.测试连接
3.通过context名称空间引入外部配置文件
习惯上我们更倾向于将上面配置的数据库连接属性抽取到一个.properties后缀的配置文件中进行统一管理,该配置文件中的所有值都是以key=value的形式存在的,之后我们就可以用context名称空间的方式在xml中以${key}的方式获取对应的值进行使用
3.1.创建db.properties文件
jdbc.username=root
jdbc.password=123456
jdbc.jdbcUrl=jdbc:mysql://192.168.1.160:3306/<u>hhisp</u>
jdbc.driverClass=com.mysql.jdbc.Driver
注意:由于username是spring的关键字,当${username}获取值时spring会获取当前电脑的登录用户,所以我们需要在properties的key之前添加前缀jdbc以便进行区分
3.2.在IOC容器中引入context的名称空间,并引入properties文件
在beans的标头中添加context名称空间引用
xmlns:context="http://www.springframework.org/schema/context"
使用context:property-placeholder标签引入配置文件
<context:property-placeholder location="classpath:db.properties"/>
注意:location属性是用来配置要解析的属性文件的url,可以配置文件的绝对路径和以’classpath:'为开头的文件的相对路径,classpath指代文件的类路径,也就是我们java工程的bin目录下
3.3.完整配置
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="db" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
</bean>
</beans>
4.spring名称空间
1.名称空间的主要作用是用来区分同名标签的
名称空间是通过给xml标签增加一个特定标识,从而区分原本可能会重复的原生xml标签的,进行区分后更有利于对xml的解析。因为只需要匹配到你想要获取的带有特定标识的标签,就可以快速定位你想要解析的标签值了
2.如何在spring的xml中引入名称空间
2.1.在beans的标头中引入名称空间的location
2.2.根据不同名称空间的使用方法来使用对应的名称空间
以p名称空间为例:p名称空间的作用和property标签是相同的,都是告诉IOC容器你要为对象赋值。(它们进行初始化时都是调用set方法)
<bean id="user" class="com.hao.spring.user.User"
p:userName="强浩" p:sex="男">
</bean>
2.3.各个名称空间的配置方法
aop
xmlns:aop="http://www.springframework.org/schema/aop"
c
xmlns:c="http://www.springframework.org/schema/c"
cache
xmlns:cache="http://www.springframework.org/schema/cache"
context
xmlns:context="http://www.springframework.org/schema/context"
jdbc
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
jee
xmlns:jee="http://www.springframework.org/schema/jee"
lang
xmlns:lang="http://www.springframework.org/schema/lang"
p
xmlns:p="http://www.springframework.org/schema/p"
task
xmlns:task="http://www.springframework.org/schema/task"
tx
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd