Maven: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.26</version> </dependency> Gradle: groovy implementation 'mysql:mysql-connector-java:8.0.26' properties DB_URL=jdbc:mysql://localhost:3306/mydatabase DB_USERNAME=root DB_PASSWORD=password DB_MAX_CONNECTIONS=100 DB_INITIAL_CONNECTIONS=10 DB_MIN_IDLE_TIME=10000 DB_MAX_IDLE_CONNECTIONS=50 DB_CONNECTION_TIMEOUT=5000 import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource; MysqlConnectionPoolDataSource dataSource = new MysqlConnectionPoolDataSource(); Properties properties = new Properties(); properties.load(new FileInputStream("config.properties")); dataSource.setURL(properties.getProperty("DB_URL")); dataSource.setUser(properties.getProperty("DB_USERNAME")); dataSource.setPassword(properties.getProperty("DB_PASSWORD")); dataSource.setMaxPoolSize(Integer.parseInt(properties.getProperty("DB_MAX_CONNECTIONS"))); dataSource.setInitialPoolSize(Integer.parseInt(properties.getProperty("DB_INITIAL_CONNECTIONS"))); dataSource.setMinIdleTime(Integer.parseInt(properties.getProperty("DB_MIN_IDLE_TIME"))); dataSource.setMaxIdleTime(Integer.parseInt(properties.getProperty("DB_MAX_IDLE_CONNECTIONS"))); dataSource.setLoginTimeout(Integer.parseInt(properties.getProperty("DB_CONNECTION_TIMEOUT"))); import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; try (Connection connection = dataSource.getConnection()) { try (Statement statement = connection.createStatement()) { ResultSet resultSet = statement.executeQuery("SELECT * FROM users"); while (resultSet.next()) { } } } catch (SQLException e) { e.printStackTrace(); }


上一篇:
下一篇:
切换中文