关键词

搭建 MySQL Maven C3P0

使用C3P0、MySQL和Maven搭建Java项目的指南

准备工作

  • 安装Java开发环境,如JDK;
  • 安装Maven;
  • 安装MySQL数据库;
  • 安装C3P0连接池;

创建Maven项目

  • 打开Eclipse,点击File -> New -> Project;
  • 选择Maven Project,点击Next;
  • 选择Create a simple project;
  • 填写Group Id、Artifact Id,点击Finish;
  • 在src/main/java目录下创建包,用来存放Java源代码;
  • 在src/main/resources目录下创建文件夹,用来存放配置文件;
  • 在src/test/java目录下创建包,用来存放测试代码;
  • 在src/test/resources目录下创建文件夹,用来存放测试配置文件;

配置Maven

  • 在pom.xml文件中添加C3P0、MySQL的依赖;
  •     <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34</version>
        </dependency>
        
  • 在pom.xml文件中添加插件;
  •     <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>2.3.2</version>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                    </configuration>
                </plugin>
            </plugins>
        </build>
        
  • 在pom.xml文件中添加资源文件;
  •     <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        

编写Java代码

  • 在src/main/resources目录下创建配置文件c3p0.properties,用于配置C3P0连接池;
  •     c3p0.driverClass=com.mysql.jdbc.Driver
        c3p0.jdbcUrl=jdbc:mysql://localhost:3306/test
        c3p0.user=root
        c3p0.password=123456
        c3p0.initialPoolSize=5
        c3p0.minPoolSize=5
        c3p0.maxPoolSize=20
        c3p0.maxIdleTime=1800
        
  • 在src/main/java目录下创建C3P0Utils类,编写获取C3P0连接池的代码;
  •     public class C3P0Utils {
            private static ComboPooledDataSource dataSource = null;
    
            static {
                try {
                    dataSource = new ComboPooledDataSource();
                    dataSource.setDriverClass(C3P0Properties.getProperty("c3p0.driverClass"));
                    dataSource.setJdbcUrl(C3P0Properties.getProperty("c3p0.jdbcUrl"));
                    dataSource.setUser(C3P0Properties.getProperty("c3p0.user"));
                    dataSource.setPassword(C3P0Properties.getProperty("c3p0.password"));
                    dataSource.setInitialPoolSize(Integer.parseInt(C3P0Properties.getProperty("c3p0.initialPoolSize")));
                    dataSource.setMinPoolSize(Integer.parseInt(C3P0Properties.getProperty("c3p0.minPoolSize")));
                    dataSource.setMaxPoolSize(Integer.parseInt(C3P0Properties.getProperty("c3p0.maxPoolSize")));
                    dataSource.setMaxIdleTime(Integer.parseInt(C3P0Properties.getProperty("c3p0.maxIdleTime")));
                } catch (PropertyVetoException e) {
                    throw new RuntimeException(e);
                }
            }
    
            public static Connection getConnection() throws SQLException {
                return dataSource.getConnection();
            }
        }
        
  • 在src/main/java目录下创建C3P0Properties类,编写读取c3p0.properties配置文件的代码;
  •     public class C3P0Properties {
            private static Properties props = new Properties();
    
            static {
                try {
                    props.load(C3P0Properties.class.getClassLoader().getResourceAsStream("c3p0.properties"));
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
    
            public static String getProperty(String key) {
                return props.getProperty(key);
            }
        }
        

编写测试代码

  • 在src/test/resources目录下创建配置文件c3p0.properties,用于配


本文链接:http://task.lmcjl.com/news/9396.html

展开阅读全文