一、JDBC介绍
- JDBC为访问不同的数据库,提供了统一接口
- 基本原理(好处
- Java驱动数据库
- 程序可以通过数据库驱动 相当于所有的可视化界面的步骤
//1.加载驱动
Class.forName
//2.用户信息和url
//3.连接成功
Connection db=DriverManager.getConnection(url, username, password);
//4.创建执行SQL的对象
Statement statement =db.createStatement();
//5.执行SQL的对象去执行SQL,可能存在结果,查看返回结果
ResultSet resultSet = statement.executeQuery(sql);
//6.释放连接
.close();
二、不同方式连接数据库
- 反射机制
Class<?> class=Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
Driver driver=(Driver)class.newInstance();//通过反射创建驱动对象
driver.connect(url,properties);//连接
- DriverManager替换Driver
Class<?> class=Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
Driver driver=(Driver)class.newInstance();//通过反射创建驱动对象
DriverManager.registerDriver(driver);//注册driver驱动
DriverManager.getConnection(url, username, password);//连接
3.省略注册driver驱动
//使用Class.forName会自动完成注册驱动
Class.forName("com.mysql.cj.jdbc.Driver");//固定写法,加载驱动
DriverManager.getConnection(url, username, password);//连接
原理:
4.可以省略加载驱动(但还是建议写上)
DriverManager.getConnection(url, username, password);//连接
5.网页端写法(省略路径问题
Properties properties = new Properties();
//通过反射来获取配置文件流(jdbc.properties必须在Resource目录底下)
InputStream inputStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties");
//并且加载
properties.load(inputStream);
//创建数据库连接池
dataSource= (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
三、数据库连接池
- 传统方式连接的问题
- 连接较多时耗时太久,占用很多系统资源
- 如果程序出现关闭异常,将可能出现内存泄漏
- 不能控制连接数量
- 连接池原理(使用DataSource接口实现
-
连接池种类
- C3P0连接池
连接方法1:读取properties配置文件,手动进行填写
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-w0LFvufd-1658969598968)(…/…/myimage/image-20220712113609946.png)]
/** * 方式1:相关参数方法 * 在程序中指定user,url,password * @throws Exception */ public static void testC3P0_1() throws Exception { //1.创建一个数据源对象 ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource(); //2.通过配置文件获取相关信息 Properties properties = new Properties(); properties.load(new FileReader("resources/jdbc.properties")); String driver=properties.getProperty("driverClassName"); String user = properties.getProperty("username"); String password = properties.getProperty("password"); String url = properties.getProperty("url"); //给comboPooledDataSource设置相关参数 comboPooledDataSource.setDriverClass(driver); comboPooledDataSource.setJdbcUrl(url); comboPooledDataSource.setUser(user); comboPooledDataSource.setPassword(password); comboPooledDataSource.setInitialPoolSize(5); comboPooledDataSource.setMaxPoolSize(10); Connection con = comboPooledDataSource.getConnection(); System.out.println("连接成功"); con.close(); }
连接方法2:使用配置模板c3p0-service.xml,需要放到根目录下
/** * 方式2:使用配置模板,需要放到根目录下 * 将使用的模板c3p0-service.xml拷贝进来 * 使用named-config 进行配置 * @throws SQLException */ public static void testC3P0_02() throws SQLException { ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("mySource"); Connection connection = comboPooledDataSource.getConnection(); System.out.println("连接成功"); connection.close(); }
运行结果:
- Druid连接池
读取properties配置文件
//1.加入jdbc.properties配置文件 Properties properties=new Properties(); properties.load(new FileReader("resources/jdbc.properties")); //2.创建数据库连接池 DataSource dataSource = DruidDataSourceFactory.createDataSource(properties); Connection connection = dataSource.getConnection(); System.out.println("连接成功"); connection.close();