1. 导入 jar 包 jar包下载地址:https://mvnrepository.com/ 
2. 配置文件 位置:project\src\database.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 #  MySQL 数据库配置 #  Druid 初始化连接数 #  Druid 最大连接数 #  Druid 最小空闲连接数 #  Druid 超时等待时间,单位:毫秒ms 
3. 连接池工具类 DruidDbUtils.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 public  class  DruidDbUtils  {private  static  DruidDataSource  druidDataSource  =  null ;private  static  final  ThreadLocal<Connection> THREAD_LOCAL = new  ThreadLocal <>();static  {Properties  properties  =  new  Properties ();InputStream  is  =  DruidDbUtils.class.getResourceAsStream("/database.properties" );try  {catch  (Exception e) {public  static  DataSource getDataSource ()  {return  druidDataSource;private  static  Connection getConnection ()  throws  SQLException {Connection  connection  =  THREAD_LOCAL.get();if  (connection == null ) {return  connection;public  static  void  begin ()  {try  {false );catch  (SQLException e) {public  static  void  commit ()  {try  {catch  (SQLException e) {public  static  void  rollback ()  {try  {catch  (SQLException e) {public  static  void  close ()  {try  {catch  (SQLException e) {public  static  void  closeAll (Connection connection, Statement statement, ResultSet resultSet)  {try  {catch  (SQLException e) {
4. 执行SQL语句 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 public  class  UserDaoImpl  implements  UserDao  {private  QueryRunner  queryRunner  =  new  QueryRunner (DruidDbUtils.getDataSource());@Override public  int  update (String action, User user)  throws  SQLException {if  (null  == action) {return  -1 ;String  sql  =  null ;null ;if  ("insert" .equals(action)) {"insert into userinfo(username, password) value(?,?)" ;new  Object []{user.getUsername(), user.getPassword()};else  if  ("delete" .equals(action)) {"delete from userinfo where id=?" ;new  Object []{user.getId()};else  if  ("update" .equals(action)) {"update userinfo set username=?,password=? where id=?" ;new  Object []{user.getUsername(), user.getPassword(), user.getId()};else  {return  -1 ;"sql="  + sql);"args="  + args);return  queryRunner.update(Objects.requireNonNull(sql), args);@Override public  User select (Integer id)  throws  SQLException {return  queryRunner.query("select * from userinfo where id=?" ,new  BeanHandler <>(User.class),@Override public  List<User> selectALL ()  throws  SQLException {return  queryRunner.query("select * from userinfo" ,new  BeanListHandler <>(User.class)