------------------------索引----------------------

定义:帮助MYSQL高效获取数据的数据结构
----------主键索引----------primary key [有唯一标识]
----------唯一索引----------unique key [避免重复的列出现,值可重复]
----------常规索引----------index/key [默认的,用index、key来设置]
----------全文索引----------fulltext [特定数据库引擎下才有,快速定位数据]

在创建表的时候,索引都是放在建表语句最下面

SHOW INDEX FROM TableName

已有表,增加索引:
alter table school.tablename ADD fulltext index indexname(studentname)

索引原则
索引不是越多越好
不要对经常变动的数据加数据
小数据量不需要加索引
索引一般加在常用查询的字段

索引的数据结构
https://blog.codinglabs.org/articles/theory-of-mysql-index.html

-----------------------Mysql的权限管理和备份--------------------

mysql.user表
修改密码:
当前:set PASSWORD = PASSWORD(‘123456’);
选择: set FOR wangzheng = PASSWORD(‘123456’);

数据库备份
备份方式:
一)、拷贝物理文件:data文件夹导出
二)、可视化工具中,手动导出
三)、mysqldump命令行导出:
mysqldump -hlocalhost -uroot -p123456 school student >D:/a.sql

navicat怎么把建表语句、数据插入语句全部导出
--------------------------- 转储SQL文件+结构与数据 -------------------------

-----------------------数据库的设计---------------------

三大范式:[信息重复、更新异常、插入异常、删除异常]
每一列都不可分
满足不可分的前提下,每张表只描述一个事情
每一列与主键直接相关,不能间接相关

----------------------------JDBC---------------------------

JAVA操作数据库的规范
java.sql
javax.sql
导入数据库驱动

第一个JDBC
导入驱动
增加lib
add as a library

package com.wang.lesson01;import javax.sql.StatementEvent;
import java.sql.*;public class JDBCFirstDemo {public static void main(String[] args) throws ClassNotFoundException, SQLException {//加载驱动,固定语法Class.forName("com.mysql.jdbc.Driver");//用户信息和URLString url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false";//这里要注意:userSLL如果用的true,系统会报错String username = "root";String password = "123456";//连接成功,数据库对象Connection connection = DriverManager.getConnection(url,username,password);//创建执行SQL的对象Statement statement = connection.createStatement();//执行SQL的对象去执行SQL,可能存在结果String sql = "select * from users ;";ResultSet resultSet = statement.executeQuery(sql);//封装了全部的while(resultSet.next()){System.out.println("id="+resultSet.getObject("id"));System.out.println("NAME="+resultSet.getObject("NAME"));System.out.println("PASSWORD="+resultSet.getObject("PASSWORD"));System.out.println("email="+resultSet.getObject("email"));System.out.println("birth="+resultSet.getObject("birthday"));System.out.println("=======================================");}//释放连接resultSet.close();statement.close();connection.close();}
}

connection代表了数据库对象
数据库自动自动提交
事务提交/回滚。这些功能都可以通过connection.commit/rollback等直接实现

statement就是执行sql对象的类
PrapareStatement也是执行sql对象的类

Statement statement = connection.createStatement();
statement.executeQuery("");
statement.execute("");
statement.executeUpdate("");

ResultSet就是封装了查询结果

resultSet.getObject("");       //如果不知道查询结果是何种类型的情况下使用resultSet.getString("");        //知道返回结果的情况下使用
resultSet.getInt("");
resultSet.getDate("");

遍历,next()

---------------单独写工具类来进行对JDBC的公共参数的封装----------

工具类代码:

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");Class.forName(driver);} catch (Exception e) {e.printStackTrace();}}//获取连接public static Connection getConnect() throws SQLException {return DriverManager.getConnection(url,username,password);}//释放连接资源public static void release(Connection connection, Statement statement, ResultSet resultSet){if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if(connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}if(statement!=null){try {statement.close();} catch (SQLException e) {e.printStackTrace();}}}
}

db.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=false
username=root
password=******

测试类

public class TestUtils {public static void main(String[] args) {Connection connection = null ;Statement statement = null ;ResultSet resultSet = null ;try {connection = JdbcUtils.getConnect();statement = connection.createStatement();String sql = "insert into users (`id`,`NAME`,`PASSWORD`,email,birthday) values (5,'梁志斌','20030707','12345678@qq.com','2020-08-02');";int i = statement.executeUpdate(sql);if (i>0){System.out.println("插入成功");}} catch (SQLException e) {e.printStackTrace();} finally {//关闭连接JdbcUtils.release(connection,statement,resultSet);}}
}

SQL注入的问题:
以用户登录为例:在username和password进行适当的SQL语句分析,试图改变原本SQL逻辑,以’or 1=1’等方式强制使SQL执行并返回结果得以实现后续登录效果等

PreparedStatement对象
可以防止SQL注入,并且效率更高

public class TestDemo {public static void main(String[] args) {Connection connection = null;PreparedStatement preparedStatement = null ;ResultSet resultSet = null ;try {connection = JdbcUtils.getConnect();//使用问号占位符String sql = "insert into users (`id`,`NAME`,`PASSWORD`,email,birthday) values (?,?,?,?,?);";preparedStatement = connection.prepareStatement(sql);//预编译sql,不执行//手动给占位符赋值preparedStatement.setInt(1,6);preparedStatement.setString(2,"卢崛");preparedStatement.setString(3,"123321");preparedStatement.setString(4,"12312312@qq.com");System.out.println((new Date().getTime())/2+"--------------------------");System.out.println(new Date().UTC(2019,12,5,12,1,3));preparedStatement.setDate(5,new java.sql.Date(new Date().getTime()));int i = 0;i = preparedStatement.executeUpdate();if (i>0){System.out.println("preparedstatement应用成功,数据已插入");}} catch (SQLException e) {e.printStackTrace();} finally {JdbcUtils.release(connection,preparedStatement,resultSet);}}
}

------------------------怎么用IDEA连接数据库--------------------------

修改mysql的时区,记得flush privileges;否则不会生效

进入后,记得在Schemas中修改一下所查看的表,这样才能看到具体的业务表

后续加紧使用联系

--------------------------------------事务--------------------------

原子、一致、隔离、持久
脏读、不可重复读、虚读(幻读)

public class TestTransFormation {public static void main(String[] args) {Connection connection = null ;PreparedStatement preparedStatement = null ;ResultSet resultSet = null ;try {connection = JdbcUtils.getConnect();//关闭MYSQL自动提交,意味着事务的开始第一步connection.setAutoCommit(false);//这里我没有建表,所以SQL暂时不执行了String sql1 = "" ;preparedStatement = connection.prepareStatement(sql1);String sql2 = "" ;preparedStatement = connection.prepareStatement(sql2);connection.commit();//这里注意,当使用了commit命令提交事务之后,数据库会自己恢复到自动提交的状态,不用刻意去开启自动提交了,同理回滚也是} catch (SQLException e) {try {//如果失败就回滚事务,其实这里不写也可以,事务中出现异常会自动回滚connection.rollback();} catch (SQLException ex) {ex.printStackTrace();}e.printStackTrace();} finally {//关闭JDBC各连接,防止占用内存JdbcUtils.release(connection,preparedStatement,resultSet);}}
}

-----------------------数据库连接池----------------------

创建连接–使用–释放十分浪费内存资源,因此提前准备好一些连接,需要使用时直接取用

实现类:DataSource

工具类TestUtiles:

public class JdbcUtils_DBCP {private static DataSource  dataSource= null ;static {try{InputStream in = JdbcUtils_DBCP.class.getClassLoader().getResourceAsStream("dbcpconfig.properties");Properties properties = new Properties();properties.load(in);//创建数据源dataSource = BasicDataSourceFactory.createDataSource(properties);} catch (Exception e) {e.printStackTrace();}}//获取连接public static Connection getConnect() throws SQLException {return dataSource.getConnection();   //从数据源中获取连接}//释放连接资源public static void release(Connection connection, Statement statement, ResultSet resultSet){if(resultSet!=null){try {resultSet.close();} catch (SQLException e) {e.printStackTrace();}}if(connection!=null){try {connection.close();} catch (SQLException e) {e.printStackTrace();}}if(statement!=null){try {statement.close();} catch (SQLException e) {e.printStackTrace();}}}
}

实现类:

public class TestUtils {public static void main(String[] args) {Connection connection = null ;Statement statement = null ;ResultSet resultSet = null ;try {connection = JdbcUtils_DBCP.getConnect();statement = connection.createStatement();String sql = "insert into users (`id`,`NAME`,`PASSWORD`,email,birthday) values (5,'梁志斌','20030707','12345678@qq.com','2020-08-02');";int i = statement.executeUpdate(sql);if (i>0){System.out.println("插入成功");}} catch (SQLException e) {e.printStackTrace();} finally {//关闭连接JdbcUtils_DBCP.release(connection,statement,resultSet);}}
}

=====区别只在于数据库连接方式的不同=

2020-08-02 Mysql数据库索引初识、备份、设计原则、JDBC连接、SQL注入、PreparedStatement对象使用、事务处理、连接池相关推荐

  1. MySQL数据库索引的最左匹配原则((a),(a,b),(a,b,c)都能用到索引,(a,c)呢?)

    MySQL数据库索引的最左匹配原则 一. 联合索引说明 二. 那ac是否能用到索引呢? 三. 思考 四. 最左匹配原则的成因 一. 联合索引说明 建立三个字段的联合索引 联合索引(a,b,c)相当于建 ...

  2. MySQL攻略 - JDBC程序SQL注入,PreparedStatement接口详解与案例练习,JDBC相关API小结

    文章目录 SQL注入 Statement详解 基本介绍 Navicat演示SQL注入 JDBC演示SQL注入 PreparedStatement详解 基本介绍 预处理好处 预处理案例(selete语句 ...

  3. MySQL 案例实战--MySQL 数据库 之 温备份 热备份

    MySQL 数据库 之 温备份 & 热备份 前言 一.完全备份方案 二.增量备份方案 三.GTID 备份 四.mydumper 备份 五.LVM 快照备份 六.xtrabackup 备份 1. ...

  4. MYSQL数据库-索引

    MYSQL数据库-索引 零.前言 一.索引概念 二.认识磁盘 三.理解索引 1.如何理解Page 2.B+ vs B 3.聚簇索引 VS 非聚簇索引 4.普通索引 5.总结 四.索引操作 1.创建索引 ...

  5. Linux实现MYSQl数据库的定时备份

    今天给大家分享一下如何在Linux下实现MYSQl数据库的定时备份. 前提需要保证你的Linux服务器已经安装了MYSQl数据库服务. 1.创建shell脚本 vim backupdb.sh 创建脚本 ...

  6. navcat定时备份mysql_Linux实现MYSQl数据库的定时备份

    今天给大家分享一下如何在Linux下实现MYSQl数据库的定时备份. 前提需要保证你的Linux服务器已经安装了MYSQl数据库服务. 1.创建shell脚本 vim backupdb.sh 创建脚本 ...

  7. 知识点:Mysql 数据库索引优化实战(4)

    知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) 一:插入订单 业务逻辑:插 ...

  8. java如何实现e的次方_Java开发如何更改MySQL数据库datadir目录之MySQL数据库索引实现...

    引言 MySQL是一个关系型数据库管理系统,由瑞典MySQL AB 公司开发,目前属于 Oracle 旗下产品.MySQL 是最流行的关系型数据库管理系统之一,在 WEB 应用方面,MySQL是最好的 ...

  9. oracle varchar默认长度_面试官:如何精确计算mysql数据库索引长度?

    概述 我们知道MySQL Innodb 对于索引长度的限制为 767 字节,并且UTF8mb4字符集是4字节字符集,则 767字节 / 4字节每字符 = 191字符(默认索引最大长度),所以在varc ...

最新文章

  1. 食品行业特点及SAP解决方案探讨
  2. 三分钟了解Mysql的表级锁——《深究Mysql锁》
  3. java抽象工厂模式_Java 抽象工厂模式
  4. javascript --- js中的作用域 变量提升
  5. ifstat命令_统计网络接口活动状态的工具
  6. 网站页面head区代码规范[转]
  7. linux网络编程学习笔记之四 -----多-threaded服务器
  8. 全国交通智慧升级,阿里云视频上云打造高速公路“视觉中枢”
  9. MKVToolNix v7.4.0 正式版
  10. 数据分析——如何构建数据指标体系
  11. 经济学中的M0 M1 M2 M3的含义
  12. 禅道----产品经理创建项目集和产品线
  13. 戴尔首推免息分期付款电脑
  14. 隐秘而伟大!知名互联网公司都在使用哪些数据库?
  15. 使用个人股票量化接口做股票投资靠谱吗?
  16. 01-JAVA基础—>赏金任务—>五子棋(面向对象)
  17. 2022年河北最新建筑八大员(材料员)模拟题库及答案
  18. HTML+CSS期末大作业——中华传统文化题材学生网页设计成品(6页面) 学生DW网页设计作业成品 web课程设计网页规划与设计 计算机毕设网页设计源码...
  19. 华硕笔记本(UEFI)支持U盘启动
  20. java毕业设计高校后勤保修系统源码+系统+数据库+lw文档+调试运行

热门文章

  1. 来回顾一下,当年的“发烧史”吧:
  2. 隧道保活超时或协商超时_隧道人员定位系统和隧道门禁系统
  3. Spring源码深度解析(郝佳)-学习-构造器注入
  4. 易保全上线“工信部查询”新功能,用户可在工信部直接查询存证数据
  5. 象棋c语言算法,中国象棋的算法是怎样的?
  6. iOS应用被杀死后继续获取用户地理位置
  7. 关于幼儿园计算机方面的知识点,幼儿园计算机教学计划
  8. 关于华科考研的一点总结
  9. oracle 在 MyBatis 中使用 like
  10. 链接读卡器 获取卡号