
事务是最小的原子单元,不能再进行拆分 在执行一条或者多条SQL语句的时候 要么都成功,要么都失败
| 指令 | 描述 |
| select @@autocommit | 查询提交方式 默认为1自动提交 0为手动提交 |
| set @@autocommit | 设置事务的提交方式(0表示手动提交 1表示自动提交)) |
| start transaction | 开启事务 |
| rollback | 回滚 |
| commit | 提交事务 |

A 事务的原子性 事务是最小的原子单位 不能进行再拆分
C 事务的一致性:事务执行sql语句 要么是同时执行成功 (成功状态) 要么同时执行失败(失败状态)
I 事务隔离性:每一个事务都是相互隔离的 互不影响
D事务持久性: 只要commit 提交之后 数据将永久保存到数据中
视图是指计算机数据库中的视图,是一个虚拟表,其内容由查询定义。同真实的表一样
看到的就是需要的。视图不仅可以简化用户对数据的理解,也可以简化他们的操作。那些被经常使用 的查询可以被定 义为视图
通过视图用户只能查询和修改他们所能见到的数据。但不能授权到数据库特定行和特定的列上
语法:create view 视图名称 as 查询语句
CREATE VIEW v_acc AS SELECT aname FROM account
语法:select * from 视图名称
SELECT * FROM v_acc
语法:alter view 视图名称 as 查询语句
ALTER VIEW v_acc AS SELECT aname,amoney FROM account
语法:drop view 视图名称
DROP VIEW v_acc
切入mysql库
查看用户表

语法:create user ‘用户名’@‘主机名’ identified by ‘密码’
CREATE USER 'lisi'@'localhost' IDENTIFIED BY '123'
语法:update user set password=password(新密码) where user=用户名
UPDATE USER SET PASSWORD =PASSWORD('456') WHERE USER='lisi'
语法:drop user ‘用户名’@‘主机名’
DROP USER 'lisi'@'localhost'
A. mysql中的密码是使用MD5来进行加密
B.md5加密方式不可逆(只能加密 不能解密) 同一个内容md5 加密都是一样的
C.网站:https://www.cmd5.com/

在实际开发中 开发人员是使用客户端连接数据库 用户使用通过界面来(使用java代码来操作)操作数据
五、jdbc操作数据库
jdbc ==> java db connection 使用java来连接数据库 使用java代码来操作数据库
jdbc是java代码操作数据库的桥梁 主要提供了一些类与接口 主要提供连接数据库的规范

1.mysql-connector-java-5.xx-bin.jar ==>适用于mysql5的版本
2.mysql-connector-java-8.xx-bin.jar ==>适用于mysql8的版本
3.下载jar地址:https://mvnrepository.com/artifact/mysql/mysql-connectorjava/8.0.28
下载对应版本的驱动包jar包,添加到项目的lib目录下,添加依赖即可
A.通过反射加载驱动
B.获取连接对象
C.根据连接对象获取执行sql的对象
D.向数据库发送sql指令
E.获取结果集
F.关闭资源
- package com.qf.test.test01;
-
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
-
- public class Test01 {
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- //通过反射加载驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
- //获取连接对象
- Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/day09?characterEncoding=utf-8",
- "root", "123456");
- //获取发送指令对象
- Statement sta = root.createStatement();
- //编写sql语句
- String sql = "insert into user(uname,upwd) values('李四','123456')";
- //发送指令
- int num = sta.executeUpdate(sql);
- System.out.println(num);
- //关闭资源
- sta.close();
- root.close();
- }
-
-
- }



- package com.qf.test01;
- import java.sql.*;
- public class Test03 {
- public static void main(String[] args) throws SQLException,
- ClassNotFoundException {
- //加载驱动
- Class.forName("com.mysql.jdbc.Driver");
- //获取连接对象
- Connection con =
- DriverManager.getConnection("jdbc:mysql://localhost:3306/day09?
- characterEncoding=utf-8", "root", "root");
- //获取执行sql对象
- Statement sta = con.createStatement();
- String sql ="select * from user ";
- //发送指令 获取结果集
- ResultSet rs = sta.executeQuery(sql);
- //使用循环获取数据
- while (rs.next()) {
- //根据索引获取
- // int uid = rs.getInt(1);
- // String uname = rs.getString(2);
- // String upwd = rs.getString(3);
- int uid = rs.getInt("uid");
- // String uname = rs.getString("uname");
- String upwd = rs.getString("upwd");
- System.out.println(uid+"\t"+"uname"+"\t"+upwd);
- }
- //关闭资源
- rs.close();
- sta.close();
- con.close();
- }
- }
需求:

代码:
- package com.qf.test.test01;
-
- import java.sql.*;
- import java.util.Scanner;
-
- public class Test02 {
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- //实例化Scanner
- Scanner input = new Scanner(System.in);
- System.out.println("请输入用户名");
- String user = input.next();
- System.out.println("请输入密码");
- String pwd = input.next();
- if (show(user,pwd)){
- System.out.println("登录成功");
- }else {
- System.out.println("登录失败");
- }
-
- }
-
- public static boolean show(String user,String pwd) throws SQLException, ClassNotFoundException {
- //加载驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
- //获取连接对象
- Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/day09?characterEncoding=utf-8", "root", "123456");
- //获取发送指令对象
- Statement sta = root.createStatement();
- //编写SQL语句
- String sql = "select * from user where uname='"+user+"' and upwd='"+pwd+"'";
- //获取结果集
- ResultSet rs = sta.executeQuery(sql);
- //判断
- if (rs.next()){
- return true;
- }
- return false;
- }
- }

PreparedStatement 是Statement的子类
PreparedStatement 会预编译 SQL语句 检测SQL语句是否正常
PreparedStatement 可以防止SQL注入问题,执行率比较高

- package com.qf.test.test01;
-
- import java.sql.*;
- import java.util.Scanner;
-
- public class Test02 {
- public static void main(String[] args) throws SQLException, ClassNotFoundException {
- //实例化Scanner
- Scanner input = new Scanner(System.in);
- System.out.println("请输入用户名");
- String user = input.next();
- System.out.println("请输入密码");
- String pwd = input.next();
- if (show(user,pwd)){
- System.out.println("登录成功");
- }else {
- System.out.println("登录失败");
- }
-
- }
-
- public static boolean show(String user,String pwd) throws SQLException, ClassNotFoundException {
- //加载驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
- //获取连接对象
- Connection root = DriverManager.getConnection("jdbc:mysql://localhost:3306/day09?characterEncoding=utf-8", "root", "123456");
- String sql = "select * from user where uname=? and upwd=?";
- PreparedStatement ps = root.prepareStatement(sql);
- //给占位符设置值
- ps.setString(1,user);
- ps.setString(2,pwd);
- //提交SQL语句
- ResultSet rs = ps.executeQuery();
- if (rs.next()){
- return true;
- }
- return false;
- }
- }
代码:
BaseDao:
- package com.qf.test.test01;
-
- import java.sql.*;
-
- public class BaseDao {
- private static final String className="com.mysql.cj.jdbc.Driver";
- private static final String driveUrl="jdbc:mysql://localhost:3306/day09?characterEncoding=utf-8";
- private static final String userName = "root";
- private static final String password="123456";
-
- //获取连接对象方法
- public static Connection getConn(){
- Connection conn = null;
- try {
- //通过反射加载驱动
- Class.forName(className);
- //获取连接对象
- conn = DriverManager.getConnection(driveUrl, userName, password);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return conn;
- }
-
- //增删改方法
- public static int update(String sql,Object[] objects){
- Connection conn=null;
- PreparedStatement ps=null;
- int num = -1;
- try {
- //获取连接对象
- conn = getConn();
- //执行SQL语句
- ps= conn.prepareStatement(sql);
- //判断数组非空
- if (objects!=null&&objects.length>0){
- for (int i=0;i
- //给占位符赋值
- ps.setObject(i+1,objects[i]);
- }
- }
- //发送SQL语句
- num = ps.executeUpdate();
- } catch (SQLException e) {
- e.printStackTrace();
- }finally {
- //关闭资源
- close(null,ps,conn);
- }
-
- return num;
- }
-
- //查询方法
- public static ResultSet getRs(String sql,Object [] objects){
- ResultSet rs = null;
- Connection conn=null;
- PreparedStatement ps=null;
- try {
- //获取连接对象
- conn = getConn();
- //执行SQL语句
- ps = conn.prepareStatement(sql);
- //判断数组非空
- if (objects!=null&&objects.length>0){
- for (int i=0;i
- //给占位符赋值
- ps.setObject(i+1,objects[i]);
- }
- }
- //发送SQL语句
- rs = ps.executeQuery();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return rs;
- }
-
- //关闭资源方法
- public static void close(ResultSet rs,PreparedStatement ps,Connection conn){
- try {
- if (rs!=null){
- rs.close();
- }
- if (ps!=null){
- ps.close();
- }
- if (conn!=null){
- conn.close();
- }
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- }
测试类:
- package com.qf.test.test01;
-
- import java.sql.ResultSet;
- import java.sql.SQLException;
-
- public class Test03 {
- public static void main(String[] args) throws SQLException {
- //增
- /*String sql = "insert into user(uname,upwd) values(?,?)";
- Object [] objects={"王五","123456789"};
- int num = BaseDao.update(sql, objects);
- System.out.println(num);*/
- //删
- /* String sql ="delete from user where uid=6";
- int num = BaseDao.update(sql, null);
- System.out.println(num);*/
- //改
- /* String sql = "update user set uname=?,upwd=? where uid=?";
- Object [] objects = {"班长","666","4"};
- BaseDao.update(sql,objects);*/
- //查
- String sql = "select * from user";
- ResultSet rs = BaseDao.getRs(sql, null);
- //遍历结果集
- while (rs.next()){
- System.out.println(rs.getInt("uid"));
- System.out.println(rs.getString("uname"));
- System.out.println(rs.getString("upwd"));
- }
- }
- }
-
相关阅读:
Nebula Graph图数据库教程介绍
2024年阿里云创建【幻兽帕鲁/Palworld】32人联机服务器教程
Acwing.4736步行者(模拟)
智能油烟机 优化烹饪体验
运筹帷幄决胜千里,Python3.10原生协程asyncio工业级真实协程异步消费任务调度实践
超全Chat GPT论文修改指令
[AutoSAR系列] 1.2 AutoSar 综述
Nginx反向代理
<二>强弱指针使用场景之 多线程访问共享对象问题
etcd之读性能主要影响因素
-
原文地址:https://blog.csdn.net/qq_53884348/article/details/126585207