
public class DataSourceDemo { public static void main(String[] args) throws SQLException { //1、创建连接池对象 JdbcDemo jd = new JdbcDemo(); System.out.println("使用之前的连接池数量:" + jd.getSize()); //2、通过连接池对象获取连接对象 Connection con = jd.getConnection(); //3、预编译查询语句 String sql = "select * from users"; PreparedStatement ps = con.prepareStatement(sql); //4、执行sql,获取结果集 ResultSet rs = ps.executeQuery(); //5、处理结果集 while (rs.next()) { System.out.println(rs.getInt("id") + "\t" + rs.getString("name") + "\t" + rs.getInt("age")); } //6、释放资源 rs.close(); ps.close(); con.close(); //这里不应该关闭后,因为关闭后连接对象无法回到连接池中继续使用,归还连接对象的方式有几种:1、装饰设计模式 2、适配器设计模式 3、动态代理方式 System.out.println("使用之后的连接池数量:" + jd.getSize()); } }
import javax.sql.DataSource; import java.io.PrintWriter; import java.sql.Connection; import java.sql.SQLException; import java.sql.SQLFeatureNotSupportedException; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.logging.Logger; /** * 自定义数据库连接池 */ public class JdbcDemo implements DataSource { //1、准备容器,用于保存连接对象。因为list是线程不安全的,使用Collections.synchronizedList变成线程安全的对象 private static List<Connection> pool = Collections.synchronizedList(new ArrayList<>()); //2、定义静态代码块,通过工具类生成10个连接池对象 static { for (int i = 0; i < 10; i++) { Connection connection = JDBCUtils.getConnection(); pool.add(connection); } } //3、获取一个连接对象 @Override public Connection getConnection() throws SQLException { //判断连接池有没有连接对象,如果有,删除0索引上的对象,获取这个从集合中删除的连接对象,这样就可以获取到一个连接对象 if (pool.size() > 0) { Connection con = pool.remove(0); return con; } else { throw new RuntimeException("连接已用完"); } } //4、定义getSize方法,获取连接池容器大小,通过该方法可以获取连接池剩余数量 public int getSize(){ return pool.size(); } @Override public Connection getConnection(String username, String password) throws SQLException { return null; } @Override public <T> T unwrap(Class<T> iface) throws SQLException { return null; } @Override public boolean isWrapperFor(Class<?> iface) throws SQLException { return false; } @Override public PrintWriter getLogWriter() throws SQLException { return null; } @Override public void setLogWriter(PrintWriter out) throws SQLException { } @Override public void setLoginTimeout(int seconds) throws SQLException { } @Override public int getLoginTimeout() throws SQLException { return 0; } @Override public Logger getParentLogger() throws SQLFeatureNotSupportedException { return null; } }
import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; /** * JDBC工具类 */ public class JDBCUtils { //1、构造方法私有化,工具类方法应该使用静态方法 private JDBCUtils() { } //2、声明所需的配置变量 private static String driverClass; //注册驱动的信息 private static String url; //MySQL的地址 private static String username; //MySQL用户名 private static String password; //MySQL密码 private static Connection con; //3、提供静态代码块,读取配置文件的信息,注册驱动 static { try { //读取配置文件的信息为变量赋值(配置文件保存在src下,通过类加载器方式获取配置文件) InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("config.properties"); Properties prop = new Properties(); prop.load(is); //赋值 driverClass = prop.getProperty("driverClass"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); //注册驱动 Class.forName(driverClass); } catch (Exception e) { throw new RuntimeException(e); } } //4、提供获取数据库连接方法 public static Connection getConnection() { try { con = DriverManager.getConnection(url, username, password); } catch (SQLException e) { throw new RuntimeException(e); } return con; } //5、提供释放资源方法 public static void close(Connection con, Statement stst, ResultSet rs) { if (con != null) { try { con.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (stst != null) { try { stst.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (rs != null) { try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
driverClass=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/db1 username=root
password=root