企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
下面提供连接数据库的三种方式,选择其中一种方式即可。 [toc] # 1. 原始方式 **1. pom.xml** ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> ``` **2. 连接代码** ```java public final class ConnUtils { public static Connection getConnection1() { try { //1. 首先加载数据库驱动 //如果使用的是mysql5,则驱动为:com.mysql.jdbc.Driver //如果使用的是mysql8,则驱动为:com.mysql.cj.jdbc.Drive Class.forName("com.mysql.jdbc.Driver"); //数据库路径 String url = "jdbc:mysql://127.0.0.1:3306/jdbc_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC"; //数据库用户名与密码 String user = "root"; String password = "root"; // 2. 连接数据库 Connection conn = DriverManager.getConnection(url, user, password); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } } ``` <br/> # 2. 借助Properties类 **1. pom.xml** ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> ``` **2. `resources/mysql.properties`** ```properties com.mysql.driver=com.mysql.jdbc.Driver com.mysql.url=jdbc:mysql://127.0.0.1:3306/jdbc_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC com.mysql.username=root com.mysql.password=root ``` **3. 连接代码** ```java public final class ConnUtils { private static String driver, username, password, url; static { Properties properties = new Properties(); InputStream is = ConnUtils.class.getClassLoader().getResourceAsStream("mysql.properties"); try { properties.load(is); driver = properties.getProperty("com.mysql.driver"); username = properties.getProperty("com.mysql.username"); password = properties.getProperty("com.mysql.password"); url = properties.getProperty("com.mysql.url"); } catch (IOException e) { e.printStackTrace(); } } public static Connection getConnection2() { try { Class.forName(driver); Connection conn = DriverManager.getConnection(url, username, password); return conn; } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return null; } } ``` <br/> # 3. 借助C3P0连接池(推荐) **1. pom.xml** ```xml <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.2</version> </dependency> <dependency> <groupId>com.mchange</groupId> <artifactId>mchange-commons-java</artifactId> <version>0.2.15</version> </dependency> ``` **2. 配置文件,选择xml或者properties都可以** * `resources/c3p0.properties` ```properties c3p0.jdbcUrl=jdbc:mysql://127.0.0.1:3306/jdbc_db?useUnicode=true&characterEncoding=UTF-8&useSSL=true&serverTimezone=UTC c3p0.driverClass=com.mysql.jdbc.Driver c3p0.user=root c3p0.password=root c3p0.acquireIncrement=3 c3p0.idleConnectionTestPeriod=60 c3p0.initialPoolSize=10 c3p0.maxIdleTime=60 c3p0.maxPoolSize=20 c3p0.maxStatements=100 c3p0.minPoolSize=5 ``` * `resources/c3p0-config.xml` ```xml <?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="jdbc_db"><!-- 代码读取的逻辑名,可以随便取 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl"> jdbc:mysql://localhost:3306/jdbc_db?useUnicode=true&amp;characterEncoding=utf-8&amp;useSSL=true&amp;serverTimezone=UTC</property> <property name="user">root</property> <property name="password">root</property> <property name="maxPoolSize">20</property> <property name="minPoolSize">3</property> <property name="maxStatements">30</property> <property name="maxIdleTime">150</property> </named-config> </c3p0-config> ``` **3. 连接代码** ```java public final class ConnUtils { //如果采用xml格式,则需要传入在xml文件创建的逻辑名 private static DataSource dataSource = new ComboPooledDataSource("jdbc_db"); //采用.properties格式,则不需要传入参数 //private static DataSource dataSource = new ComboPooledDataSource(); public static Connection getConnection3() { try { return dataSource.getConnection(); } catch (SQLException e) { e.printStackTrace(); } return null; } } ```