当前位置:网站首页>MySQL——JDBC
MySQL——JDBC
2022-07-03 21:06:00 【Java学不会】
文章目录
1、数据库驱动
- 程序需要通过数据库驱动,和数据库打交道
2、JDBC
- SUN公司为了简化开发人员的(对数据的统一)操作,提供了一个(Java操作数据库的)规范,俗称JDBC
- 对于开发人员,只需要掌握JDBC接口操作即可
3、第一个JDBC程序
创建测试数据库
CREATE DATABASE `jdbcStudy` CHARACTER SET utf8 COLLATE utf8_general_ci; USE `jdbcStudy`; CREATE TABLE `users`( `id` INT PRIMARY KEY, `NAME` VARCHAR(40), `PASSWORD` VARCHAR(40), `email` VARCHAR(60), birthday DATE ); INSERT INTO `users`(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) VALUES('1','zhangsan','123456','[email protected]','1980-12-04'), ('2','lisi','123456','[email protected]','1981-12-04'), ('3','wangwu','123456','[email protected]','1979-12-04')
导入数据库驱动
编写测试代码
package demo; import java.sql.*; //第一个JDBC程序 public class JdbcFirstDemo { public static void main( String[] args ) throws ClassNotFoundException, SQLException { //1.加载驱动 Class.forName("com.mysql.jdbc.Driver"); //固定写法,加载驱动 //2.连接信息 String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false"; String username = "root"; String password = "123456"; //3.连接成功 数据库对象 Connection 代表数据库 Connection connection = DriverManager.getConnection(url, username, password); //4.执行SQL的对象 Statement 执行sql的对象 Statement statement = connection.createStatement(); //5.执行SQL的对象 去 执行SQL,可能存在结果,查看返回结果 String sql="SELECT * FROM users"; ResultSet resultSet = statement.executeQuery(sql); //返回结果集,结果集封装了全部查询出来的结果 while (resultSet.next()){ System.out.println("id="+resultSet.getObject(1)); System.out.println("name="+resultSet.getObject(2)); System.out.println("pwd="+resultSet.getObject(3)); System.out.println("email="+resultSet.getObject(4)); System.out.println("birthday="+resultSet.getObject(5)); System.out.println("======================"); } //6.释放连接 resultSet.close(); statement.close(); connection.close(); } }
4、步骤总结
- 加载驱动
- 连接数据库 DriverManager
- 获得执行sql的对象 Statement
- 获得返回的结果集
5、JDBC对象解释
- Class.forName(“com.mysql.jdbc.Driver”); //加载驱动
- connection: 数据库对象,获得一个数据库
- statement: 执行SQL语句
- statement.executeQuery(sql) //查询语句,返回ResultSet结果集
- statement.executeUpdate(sql) //更新语句,返回更新的行数
- statement.execute(sql) //任何SQL语句
- ResultSet: SQL查询结果集,根据数据库的类型
- resultSet.next() //指针,移动到下一个数据
- resultSet.getInt(参数); //获得整型数据,可以是下标(从1开始),也可以是"表字段名称"
- resultSet.getString(参数); //获得字符串数据,可以是下标(从1开始),也可以是"表字段名称"
- resultSet.getObject(参数); //获得任意型数据,可以是下标(从1开始),也可以是"表字段名称"
- 释放资源:
- resultSet.close();
- statement.close();
- connection.close();
6、写工具类
在src目录下创建
db.properties
文件driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false username = root password = 123456
创建工具类
JdbcUtils
package demo; import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.util.Properties; public class JdbcUtils { private static String driver=null; private static String url=null; private static String username=null; private static String password=null; static { try{ InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("db.properties"); Properties properties = new Properties(); properties.load(in); driver= properties.getProperty("driver"); url= properties.getProperty("url"); username= properties.getProperty("username"); password= properties.getProperty("password"); // 1.驱动只用加载一次 Class.forName(driver); }catch (IOException e){ } catch (ClassNotFoundException e) { e.printStackTrace(); } } // 获取连接 public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, username, password); } //释放资源 public static void release( Connection conn, Statement st,ResultSet rs ){ if(rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
使用工具类
package demo; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestInsert { public static void main( String[] args ) { Connection conn=null; Statement st=null; ResultSet rs=null; try { conn = JdbcUtils.getConnection();//获得数据库连接 st = conn.createStatement(); //获得SQL的执行对象 String sql="INSERT\tINTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " + "VALUES(4,'Java学不会','123456','[email protected]','2022-07-02')"; int i = st.executeUpdate(sql); if(i>0){ System.out.println("插入成功"); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JdbcUtils.release(conn,st,rs); } } }
7、JDBC操作事务
7.1、创建表
-- 创建用户表
create table account(
id int primary key auto_increment,
name varchar(40),
money float
);
-- 插入数据
insert into account(name,money) values ('A',1000);
insert into account(name,money) values ('B',1000);
insert into account(name,money) values ('C',1000);
7.2、事务
要么都成功,要么都失败
ACID原则:
- 原子性:要么全部成功,要么全部失败
- 一致性:总数不变
- 隔离性:多个线程互不干扰
- 脏读:一个事务读取了另一个没有提交的事务
- 不可重复读:在同一个事务内,重复读取表中的数据,表数据发生了改变
- 虚读(幻读):在一个事务内,读取到了别人插入的数据,导致前后读出来结果不一致
- 持久性:一旦提交不可逆,持久化到数据库
package demo01; import demo.JdbcUtils; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class TestTransaction01 { public static void main( String[] args ) { Connection conn=null; PreparedStatement st=null; ResultSet rs=null; try { conn = JdbcUtils.getConnection(); //关闭数据库的自动提交,自动会开启事务 conn.setAutoCommit(false); String sql1="update account set money = money-100 where name ='A'"; st = conn.prepareStatement(sql1); st.execute(); int x = 1/0; //报错 String sql2="update account set money = money+100 where name ='B'"; st = conn.prepareStatement(sql2); st.execute(); //提交事务 conn.commit(); System.out.println("操作成功"); } catch (SQLException throwables) { try { conn.rollback(); //如果失败就回滚,默认会回滚 } catch (SQLException e) { e.printStackTrace(); } } finally { JdbcUtils.release(conn,st,rs); } } }
7.3、总结
- 开始事务: conn.setAutoCommit(false);
- 一组业务执行完毕,提交事务
- 可以在catch语句中显示的定义,回滚语句,默认失败就会回滚
8、数据库连接池
- 数据库连接——执行完毕——释放
- 连接——释放十分浪费系统资源
- 池化技术:准备一些预先的资源,过来就连接预先准备好的
- 最小连接数
- 最大连接数
- 等待超时
- 编写连接池,实现一个接口 DataSource
- 开源数据源实现
- DBCP
- C3P0
- Druid
- 使用了这些数据库连接池只有,在项目开发中就不需要编写连接数据库的代码了
8.1 、DBCP:
创建 dbcpconfig.properties 文件
#连接设置 这里面的名词,是DBCP数据源中定义好的 driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/jdbcstudy?userUnicode=true&characterEncoding=utf8&useSSL=false username=root password=123456 #<!-- 初始化连接 --> initialSize=10 #最大连接数量 maxActive=50 #<!-- 最大空闲连接 --> maxIdle=20 #<!-- 最小空闲连接 --> minIdle=5 #<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 --> maxWait=60000 #JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:【属性名=property;】 #注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。 connectionProperties=useUnicode=true;characterEncoding=utf8 #指定由连接池所创建的连接的自动提交(auto-commit)状态。 defaultAutoCommit=true #driver default 指定由连接池所创建的连接的只读(read-only)状态。 #如果没有设置该值,则“setReadOnly”方法将不被调用。(某些驱动并不支持只读模式,如:Informix) defaultReadOnly= #driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。 #可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE defaultTransactionIsolation=READ_COMMITTED
工具类
package demo02; import demo.JdbcUtils; import org.apache.commons.dbcp2.BasicDataSource; import org.apache.commons.dbcp2.BasicDataSourceFactory; import java.io.InputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; public class JdbcUtils_DBCP { private static BasicDataSource dataSource = null; static { try{ InputStream in = JdbcUtils.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"); Properties properties = new Properties(); properties.load(in); //创建数据源 工厂模式-->创建对象 dataSource = BasicDataSourceFactory.createDataSource(properties); }catch (Exception e){ } } // 获取连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); //从数据源中获取连接 } //释放资源 public static void release( Connection conn, Statement st, ResultSet rs ){ if(rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
测试代码
package demo02; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class TestDBCP { public static void main( String[] args ) { Connection conn=null; Statement st=null; ResultSet rs=null; try { conn = JdbcUtils_DBCP.getConnection();//获得数据库连接 st = conn.createStatement(); //获得SQL的执行对象 String sql="INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " + "VALUES(6,'JavaStudy','123456','[email protected]','2022-07-02')"; int i = st.executeUpdate(sql); if(i>0){ System.out.println("插入成功"); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JdbcUtils_DBCP.release(conn,st,null); } } }
8.2、c3p0
创建 c3p0-config.xml 文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <default-config> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?userUnicode=true&characterEncoding=utf8&useSSL=false&</property> <property name="user">root</property> <property name="password">123456</property> <property name="acquiredIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </default-config> <name-config name="MySQL"> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?userUnicode=true&characterEncoding=utf8&useSSL=false&</property> <property name="user">root</property> <property name="password">123456</property> <property name="acquiredIncrement">5</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">5</property> <property name="maxPoolSize">20</property> </name-config> </c3p0-config>
工具类:
package demo02; import com.mchange.v2.c3p0.ComboPooledDataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class JdbcUtils_c3p0 { private static ComboPooledDataSource dataSource = null; static { try{ //创建数据源 工厂模式-->创建对象 dataSource = new ComboPooledDataSource(); }catch (Exception e){ } } // 获取连接 public static Connection getConnection() throws SQLException { return dataSource.getConnection(); //从数据源中获取连接 } //释放资源 public static void release( Connection conn, Statement st, ResultSet rs ){ if(rs!=null){ try { rs.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(st!=null){ try { st.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException throwables) { throwables.printStackTrace(); } } } }
测试代码:
package demo02; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; public class Testc3p0 { public static void main( String[] args ) { Connection conn=null; Statement st=null; ResultSet rs=null; try { conn = JdbcUtils_c3p0.getConnection();//获得数据库连接 st = conn.createStatement(); //获得SQL的执行对象 String sql="INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`) " + "VALUES(10,'JavaStudy','123456','[email protected]','2022-07-02')"; int i = st.executeUpdate(sql); if(i>0){ System.out.println("插入成功"); } } catch (SQLException throwables) { throwables.printStackTrace(); } finally { JdbcUtils_DBCP.release(conn,st,null); } } }
边栏推荐
- 18、 MySQL -- index
- leetcode-540. A single element in an ordered array
- Design e-commerce seckill system
- Custom view incomplete to be continued
- Leetcode daily question 540 A single element in an ordered array Valentine's Day special article looking for a single dog in a pile of lovers ~ the clown is myself
- Advanced collaboration: coroutinecontext
- 抓包整理外篇——————autoResponder、composer 、statistics [ 三]
- Discussion Net legacy application transformation
- Node MySQL serialize cannot rollback transactions
- "Designer universe" argument: Data Optimization in the design field ultimately falls on cost, safety and health | chinabrand.com org
猜你喜欢
"Actbert" Baidu & Sydney University of technology proposed actbert to learn the global and local video text representation, which is effective in five video text tasks
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?
Basic preprocessing and data enhancement of image data
Qtablewidget control of QT
全网都在疯传的《老板管理手册》(转)
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
Apprentissage intensif - notes d'apprentissage 1 | concepts de base
Discussion Net legacy application transformation
jvm jni 及 pvm pybind11 大批量数据传输及优化
随机推荐
QT6 QML book/qt quick 3d/ Basics
[Yugong series] go teaching course 002 go language environment installation in July 2022
浅议.NET遗留应用改造
Recommendation of books related to strong foundation program mathematics
Design e-commerce seckill system
大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
Visiontransformer (I) -- embedded patched and word embedded
Operate BOM objects (key)
Pytorch sets the weight and bias of the model to zero
Qualcomm platform WiFi -- P2P issue
In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
@Scenario of transactional annotation invalidation
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
@Transactional注解失效的场景
Cannot load driver class: com. mysql. cj. jdbc. Driver
Discussion Net legacy application transformation
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
Hcie security Day12: supplement the concept of packet filtering and security policy
Pengcheng cup Web_ WP
CesiumJS 2022^ 源码解读[7] - 3DTiles 的请求、加载处理流程解析