1. 导入 jar 包 jar包下载地址:https://mvnrepository.com/
导入IDEA,位置:project\lib
2. 配置文件 位置:project\src\database.properties
1 2 3 4 5 6 7 8 9 10 11 12 13 # MySQL 数据库配置 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/数据库名?useUnicode=true&characterEncoding=utf8 username=root password=123456# Druid 初始化连接数 initialSize=10# Druid 最大连接数 maxActive=30# Druid 最小空闲连接数 minIdle=5# Druid 超时等待时间,单位:毫秒ms maxWait=5000
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 { properties.load(is); druidDataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static DataSource getDataSource () { return druidDataSource; } private static Connection getConnection () throws SQLException { Connection connection = THREAD_LOCAL.get(); if (connection == null ) { connection = druidDataSource.getConnection(); THREAD_LOCAL.set(connection); } return connection; } public static void begin () { try { getConnection().setAutoCommit(false ); } catch (SQLException e) { e.printStackTrace(); } } public static void commit () { try { getConnection().commit(); } catch (SQLException e) { e.printStackTrace(); } } public static void rollback () { try { getConnection().rollback(); } catch (SQLException e) { e.printStackTrace(); } } public static void close () { try { Objects.requireNonNull(getConnection()).close(); } catch (SQLException e) { e.printStackTrace(); } } public static void closeAll (Connection connection, Statement statement, ResultSet resultSet) { try { Objects.requireNonNull(connection).close(); Objects.requireNonNull(statement).close(); Objects.requireNonNull(resultSet).close(); } catch (SQLException e) { e.printStackTrace(); } } }
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 ; Object[] args = null ; if ("insert" .equals(action)) { sql = "insert into userinfo(username, password) value(?,?)" ; args = new Object []{user.getUsername(), user.getPassword()}; } else if ("delete" .equals(action)) { sql = "delete from userinfo where id=?" ; args = new Object []{user.getId()}; } else if ("update" .equals(action)) { sql = "update userinfo set username=?,password=? where id=?" ; args = new Object []{user.getUsername(), user.getPassword(), user.getId()}; } else { return -1 ; } System.out.println("sql=" + sql); System.out.println("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), id ); } @Override public List<User> selectALL () throws SQLException { return queryRunner.query( "select * from userinfo" , new BeanListHandler <>(User.class) ); } }