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 + +