From 91491824ec21cfa21d06ddd004de8887880bcdd4 Mon Sep 17 00:00:00 2001 From: liyansheng <1761724207@qq.com> Date: Sat, 21 Dec 2024 23:24:33 +0800 Subject: [PATCH] =?UTF-8?q?fix<=E8=B0=83=E6=95=B4=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E9=93=BE=E6=8E=A5=E6=BA=90>?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/example/dao/UserDao.java | 2 +- src/main/java/example/utils/DBUtils.java | 18 +++--------------- src/main/resources/c3p0-config.xml | 9 +++++++++ 3 files changed, 13 insertions(+), 16 deletions(-) create mode 100644 src/main/resources/c3p0-config.xml diff --git a/src/main/java/example/dao/UserDao.java b/src/main/java/example/dao/UserDao.java index 8d4cfe5..1a55dce 100644 --- a/src/main/java/example/dao/UserDao.java +++ b/src/main/java/example/dao/UserDao.java @@ -43,7 +43,7 @@ public class UserDao { public static void main(String[] args) throws Exception { UserDao userDao = new UserDao(); - System.out.println(userDao.getUserById(1).getUsername()); + System.out.println(userDao.getAllUser()); } } diff --git a/src/main/java/example/utils/DBUtils.java b/src/main/java/example/utils/DBUtils.java index e655f9f..e1269a2 100644 --- a/src/main/java/example/utils/DBUtils.java +++ b/src/main/java/example/utils/DBUtils.java @@ -3,22 +3,10 @@ package example.utils; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DBUtils { - private static ComboPooledDataSource dataSource; - - static { - try { - dataSource = new ComboPooledDataSource(); - dataSource.setDriverClass("com.mysql.cj.jdbc.Driver"); - dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/work?useSSL=false&serverTimezone=UTC"); - dataSource.setUser("root"); - dataSource.setPassword("root"); - } catch (Exception e) { - e.printStackTrace(); - } - } + // 使用默认配置名 "default-config" 从c3p0-config.xml文件中获取数据源配置 + private static ComboPooledDataSource dataSource = new ComboPooledDataSource("default-config"); public static ComboPooledDataSource getDataSource() { return dataSource; } - -} +} \ No newline at end of file diff --git a/src/main/resources/c3p0-config.xml b/src/main/resources/c3p0-config.xml new file mode 100644 index 0000000..89bc494 --- /dev/null +++ b/src/main/resources/c3p0-config.xml @@ -0,0 +1,9 @@ + + + + com.mysql.cj.jdbc.Driver + jdbc:mysql://localhost:3306/work?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8&useSSL=false + root + root + +