我们平时利用持久化架构就完全足够我们使用数据库了,可是就和学习MVC前要先学javaweb一样,我们可以学会利用驱动或者连接池方式去操纵库,这样既可以提升我们对驱动和连接池的认识,也可以更好的帮助理清 驱动、连接池、持久化框架之间的关系。当然这只是其中一节内容
手写 通过驱动直连数据库
public static Connection getConnection() throws Exception {
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
Properties pros = new Properties();
pros.load(is);
String user = pros.getProperty("user");
String password = pros.getProperty("password");
String url = pros.getProperty("url");
String driverClass = pros.getProperty("driverClass");
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
}
手写 通过连接池连接数据库
- Hikari连接池
Properties properties = new Properties();
InputStream in = DBService.class.getClass().getResourceAsStream(DB_CONFIG_FILE);
properties.load(in);
db_max_conn = Short.valueOf(properties.getProperty("db_max_conn"));
db_url = String.valueOf(properties.getProperty("db_url"));
db_port = Short.valueOf(properties.getProperty("db_port"));
db_name = String.valueOf(properties.getProperty("db_name"));
db_username = String.valueOf(properties.getProperty("db_username"));
db_password = String.valueOf(properties.getProperty("db_password"));
if (db_url == null || db_url.length() == 0) {
logger.error("配置的数据库ip地址错误!");
System.exit(0);
}
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(db_max_conn);
config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
config.addDataSourceProperty("serverName", db_url);
config.addDataSourceProperty("port", db_port);
config.addDataSourceProperty("databaseName", db_name);
config.addDataSourceProperty("user", db_username);
config.addDataSourceProperty("password", db_password);
dataSource = new HikariDataSource(config);
- druid连接池
//数据源配置
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl("jdbc:mysql://localhost:3306/test");
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver"); //这个可以缺省的,会根据url自动识别
dataSource.setUsername("wzc");
dataSource.setPassword("123456");
//可选配置
dataSource.setInitialSize(10); //初始连接数,默认0
dataSource.setMaxActive(30); //最大连接数,默认8
dataSource.setMinIdle(10); //最小闲置数
dataSource.setMaxWait(2000); //获取连接的最大等待时间,单位毫秒
dataSource.setPoolPreparedStatements(true); //缓存PreparedStatement,默认false
dataSource.setMaxOpenPreparedStatements(20); //缓存PreparedStatement的最大数量,默认-1(不缓存)。大于0时会自动开启缓存PreparedStatement,所以可以省略上一句代码
//获取连接
Connection connection = dataSource.getConnection();
//查询
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from user");
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
System.out.print(id + " ");
System.out.print(name + " ");
}
//关闭连接
connection.close();
- c3p0连接池
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
cpds.setUser("wzc");
cpds.setPassword("123456");
cpds.setMaxStatements(180);
Connection connection = cpds.getConnection();
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("select * from user");
while(rs.next()){
String id = rs.getString("id");
String name = rs.getString("name");
System.out.print(id + " ");
System.out.print(name + " ");
}
rs.close();
DataSources.destroy(cpds);
我们在使用连接池,还是要利用驱动的方式,去构建连接