1.首先简单介绍一下java本身获取数据源对象的方式。
public class JDBCForDBConn {
public static void main(String[] args) {
String driver = "com.mysql.jdbc.Driver";
String dbName = "student";
String userName = "root";
String password = "123456";
String url = "jdbc:mysql://localhost:3306/"+dbName;
String sql = "select * from student";
try {
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, userName, password);
PreparedStatement pstat = conn.prepareStatement(sql);
ResultSet rs = pstat.executeQuery();
while(rs.next()){
System.out.println("id:"+rs.getInt(1)+" name:"+rs.getString(2)+" age:"+rs.getInt(3));
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
通过DriverManager的getConnection(url,name,password)获取数据库连接对象。实现数据库连接。
2.spring框架中可使用以下方式获取DataSource数据源。
(1).JNDI方式
在本地tomcat服务器中配置JNDI信息。这个可以通过在根目录conf中的server.xml或者context.xml中配置。也可以在项目META-INF下新建一个context.xml文件,进行相关配置(没有该目录的话可以自己新建一个,与WEB-INF同级)。具体配置内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource name="jdbc/mysql" type="javax.sql.DataSource" auth="Container"
username="root" password="123456" driverClassName="com.mysql.jdbc.Driver" maxIdle="3000" maxWait="5000"
url="jdbc:mysql://localhost:3306/student" autoReconnect="true" maxActive="1000"/>
</Context>
以上为META-INF下新建的context.xml内容。如果是在tomcat目录下,则直接将Resource标签内容配置到Context标签下就可以了。
然后在项目web.xml目录下引入相关配置。
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
此时如果不在配置文件中进行spring相关配置,则可以通过以下方式获取数据源。
Context context = new InitialContext();
DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/mysql");
Connection conn = ds.getConnection();
通过spring配置,可以在xml中进行如下配置。
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/mysql</value>
</property>
</bean>
然后在代码中可以通过如下方式获取
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource)ac.getBean("dataSource");
注意:
1.由于在tomcat服务器中进行了相关配置,所以需要通过tomcat服务器启动项目。
2.applicationContext.xml放在classes目录下。
(2).使用DriverManagerDataSource
使用DriverManagerDataSource需要在xml文件中进行如下配置:
<bean id="dataSource1"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3306/student</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
然后java获取数据源方式与上面springJNDI方式相同。
(3).使用第三方连接池获取
第三方连接池比较常用的有DBCP和C3P0。使用DBCP需要引入jar包:commons-collections,commons-dbcp,commons-pool。使用C3P0需要引入jar包c3p0。xml中配置如下:
<bean id="dataSource2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/student"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
<property name="maxActive" value="100"></property>
<property name="maxIdle" value="30"></property>
<property name="maxWait" value="10"></property>
<property name="defaultAutoCommit" value="false"></property>
</bean>
<bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="jdbcUrl">
<value>jdbc:mysql://localhost:3306/student</value>
</property>
<property name="user">
<value>root</value>
</property>
<property name="password">
<value>123456</value>
</property>
</bean>
然后java获取数据源方式与上面springJNDI方式相同。