配置数据源的三种方式

1. 在TomCat中配置好数据源,然后使用java自带的类获取连接

  • 使用步骤:

    • 在D:\apache-tomcat-8.5.32\conf\server.xml 中配置好JNDI数据源
    	
    <Resource 
            name="jdbc/mysql"
            auth="Container" 
            type="javax.sql.DataSource"
            maxActive="100" 
            maxIdle="30" 
            maxWait="10000"
            username="root" 
            password="abc123"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/test_db"/>
    
    • 在 D:\apache-tomcat-8.5.32\conf\Catalina\ 目录下创建应用对应的配置文件,如localhost\spring01.xml,即与映射路径对应
    <Context>
    
        
        <ResourceLink name="mysqlDataSource" global="jdbc/mysql" type="javax.sql.DataSource"/>
    
    Context>
    
    • 在应用web.xml 文件中引用JNDI 数据源
      
      <resource-ref>
        <res-ref-name>mysqlDataSourceres-ref-name>
        <res-type>javax.sql.DataSourceres-type>
        <res-auth>Containerres-auth>
      resource-ref>
    
    • 使用java 自带的类连接JNDI 数据源
    import javax.naming.Context;
    import javax.naming.InitialContext;
    import javax.naming.NamingException;
    
    		Connection connection = null;
            try {
                Context context = new InitialContext();
                DataSource source = (DataSource) context.lookup("java:comp/env/mysqlDataSource");
                connection = source.getConnection();
                ResultSet resultSet = connection.createStatement().
                        executeQuery("select * from tb_emp1");
                while (resultSet.next()) {
                    System.out.println(resultSet.getInt("id") + " "
                    + resultSet.getString("name") + " "
                    + resultSet.getInt("depId") + " "
                    + resultSet.getString("salary"));
                }
                context.close();
                connection.close();
            } catch (NamingException e) {
                e.printStackTrace();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    

2. 在TomCat中配置好数据源,然后使用spring 提供的bean 获取连接

  • 使用步骤:

    • 配置数据源的步骤同第一种方式
    • 配置好数据源后,获取jndi bean
    <bean id="datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
            <property name="jndiName">
                <value>java:comp/env/mysqlDataSource</value>
            </property>
    </bean>
    
            try {
                ApplicationContext context = new ClassPathXmlApplicationContext("spring-cfg1.xml");
                DataSource dataSource = (DataSource) context.getBean("datasource");
                Connection connection = dataSource.getConnection();
                ResultSet resultSet = connection.createStatement().
                        executeQuery("select * from tb_emp1");
                while (resultSet.next()) {
                    System.out.println(resultSet.getInt("id") + " "
                    + resultSet.getString("name") + " "
                    + resultSet.getInt("depId") + " "
                    + resultSet.getString("salary"));
                }
    
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    

3. 使用c3p0

  • 使用步骤:

    • 在pom.xml 中加入依赖
            
            <dependency>
                <groupId>com.mchangegroupId>
                <artifactId>c3p0artifactId>
                <version>0.9.5.2version>
            dependency>
    

    这个依赖会导入两个jar包:c3p0-0.9.5.2.jar mchange-commons-java-0.2.11.jar

    • 在spring 配置文件中配置c3p0 数据源bean
    <bean id="c3p0datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
            <property name="driverClass" value="com.mysql.jdbc.Driver"/>
            <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test_db"/>
            <property name="user" value="root"/>
            <property name="password" value="abc123"/>
    bean>
    

    destroy-method=“close” 表示spring 容器关闭时,c3p0 会调用自己的close 方法释放资源

    • 通过c3p0 数据源获取数据库连接
            ApplicationContext context = new ClassPathXmlApplicationContext("spring-cfg1.xml");
            ComboPooledDataSource comboPooledDataSource = (ComboPooledDataSource) context.getBean("c3p0datasource");
            try {
                Connection connection = comboPooledDataSource.getConnection();
                ResultSet resultSet = connection.createStatement().
                        executeQuery("select * from tb_emp1");
                while (resultSet.next()) {
                    System.out.println(resultSet.getInt("id") + " "
                    + resultSet.getString("name") + " "
                    + resultSet.getInt("depId") + " "
                    + resultSet.getString("salary"));
                }
    
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
    

你可能感兴趣的:(服务器配置+java后端)