文章
MySql
于2021-3-24创建本文档
一、基本操作
1.创建一个表
CREATE TABLE IF NOT EXISTS `school`(
`id` INT(4) NOT NULL AUTO_INCREMENT COMMENT '学号',
`name` VARCHAR(30) NOT NULL DEFAULT '匿名' COMMENT '姓名',
`pwd` VARCHAR(20) NOT NULL DEFAULT '123456' COMMENT '密码',
`birthday` DATETIME DEFAULT NULL COMMENT '生日',
PRIMARY KEY(`id`)
)ENGINE=INNODB DEFAULT CHARSET=utf8
** 增加外键约束
2.常用命令
查看创建语句
3.修改和删除表
4.导出sql
5.指令合集
二. 数据库特性
1.数据类型
2.字段属性
3.数据表
4.三大范式
三.DML操作语言
数据库意义 :数据储存,数据管理
DML语言,数据操作语言
1.插入语句
2.修改语句
3.删除
4.where操作字句
四.DQL查询语言
1.Select完整语法概览
2.查询指定字段
3.模糊查询
4.联表查询
5.分组,过滤和自连接
HAVING…
自连接
6.分页和排序
7.子查询
8.常用时间函数
9.聚合函数
10.MD5加密
五. JDBC
0.工具类,mysql加载器unit_0
在,2…中用到
package mysql;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
//工具类,mysql加载器
public class utils_0 {
private static String driver = null;
private static String url = null;
private static String username = null;
private static String pwd = null;
static {
try {
InputStream in = utils_0.class.getClassLoader().getResourceAsStream("db.properties");
Properties properties = new Properties();
properties.load(in);
driver = properties.getProperty("driver");
url = properties.getProperty("url");
username = properties.getProperty("username");
pwd = properties.getProperty("pwd");
//驱动只用一次
Class.forName(driver);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
//获取连接
public static Connection connector() throws SQLException {
return DriverManager.getConnection(url,username,pwd);
}
//释放资源
public static void release(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
if(resultSet!=null)
{
resultSet.close();
}
if(statement!=null)
{
statement.close();
}
if(connection!=null)
{
connection.close();
}
}
}
1.加载及简单操作
package mysql;
import javax.swing.plaf.nimbus.State;
import java.sql.*;
public class Jdbc_test_1 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.加载驱动
Class.forName("com.mysql.jdbc.Driver");//固定写法
//2.用户信息和url
String url = "jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true";
String username = "root";
String pwd = "123456";
//3.连接成功,返回数据库对象
Connection connection = DriverManager.getConnection(url,username,pwd);
//4.执行sql对象
Statement statement = connection.createStatement();
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"));
}
//释放连接
resultSet.close();
statement.close();
connection.close();
}
}
2.增删改操作
package mysql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class testSelect_1 {
public static void main(String[] args) throws SQLException {
Connection connection = utils_0.connector();//加载工具类
Statement st = connection.createStatement();
ResultSet resultSet = null;
//插入
//String sql = "INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)" + "VALUES(5,'grys','123456','44.qq.com','2001-3-13') ";
//删除
//String sql = "DELETE FROM `users` WHERE id = 5";
//改变
String sql = "select * from users where id = 4";
resultSet = st.executeQuery(sql);
if(resultSet.next())
{
System.out.println(resultSet.getString("name"));
}
utils_0.release(connection,st,resultSet);
}
}
3.sql注入
package mysql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class SqlInsert_2 {
public static void main(String[] args) throws SQLException {
//正常登录
//login("star","123456");
//sql注入
login("'or ' 1=1","123456");
//安全的方法:prepareStatement 见 3
}
public static void login(String name,String pwd) throws SQLException {
Connection connection = utils_0.connector();
Statement st = connection.createStatement();
ResultSet resultSet = null;
String sql = "select * from users where `NAME`='" +name+"'AND `PASSWORD` = '"+pwd+"'";
resultSet = st.executeQuery(sql);
while(resultSet.next())
{
System.out.println(resultSet.getString("name"));
}
utils_0.release(connection,st,resultSet);
}
}
4.sql预处理
在实际应用中有些sql语句需要等待程序的返回值等信息才可执行,这里可以使用sql预处理
使用’?'占位符
package mysql;
import javax.rmi.CORBA.Util;
import java.sql.Connection;
//import java.sql.Date;
import java.util.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class PrepareStatement_3 {
public static void main(String[] args) throws SQLException {
Connection connection = null;
PreparedStatement st = null;
connection = utils_0.connector();
//使用?占位符代替参数
String sql = "INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)" + "VALUES(?,?,?,?,?) ";
st = connection.prepareStatement(sql);//sql预处理,不执行
//手动给参数赋值
st.setInt(1,9);
st.setString(2,"xingxing");
st.setString(3,"xiaogu");
st.setString(4,"8268@qq.com");
//sql.Data()数据库时间
//Util.Data()java时间
st.setDate(5,new java.sql.Date(new Date().getTime()));
int i = st.executeUpdate();
if(i>0)
{
System.out.println("插入成功");
}
utils_0.release(connection,st,null);
}
}
5.改
为了防止脏读,需要设置异常回滚
package mysql;
import javax.rmi.CORBA.Util;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class transation_4 {
public static void main(String[] args) throws SQLException {
Connection conn = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
try {
conn = utils_0.connector();
conn.setAutoCommit(false);
String sql1 = "update `account` set money=money-100 where name = 'A'";
prepareStatement = conn.prepareStatement(sql1);
prepareStatement.executeUpdate();
String sql2 = "update `account` set money=money+100 where name = 'B'";
prepareStatement = conn.prepareStatement(sql2);
prepareStatement.executeUpdate();
conn.commit();//要等到两条语句都成功执行才保存,否则回滚
System.out.println("成功");
} catch (SQLException throwables) {
throwables.printStackTrace();
try {
conn.rollback();//回滚
} catch (SQLException e) {
e.printStackTrace();
}
} finally {
utils_0.release(conn,prepareStatement,rs);
}
}
}
6.dbcp池化技术
DBCP(DataBase connection pool)数据库连接池是 apache 上的一个Java连接池项目。DBCP通过连接池预先同数据库建立一些连接放在内存中(即连接池中),应用程序需要建立数据库连接时直接到从接池中申请一个连接使用,用完后由连接池回收该连接,从而达到连接复用,减少资源消耗的目的。
池化类
package mysql;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
public class utils_dbcp_5 {
private static DataSource dataSource = null;
static {
try {
InputStream in = utils_0.class.getClassLoader().getResourceAsStream("dbcp.properties");
Properties properties = new Properties();
properties.load(in);
//创建数据源 工厂模式-->创建对象
dataSource = BasicDataSourceFactory.createDataSource(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection connector() throws SQLException {
return dataSource.getConnection(); //从数据源中获取连接
}
//释放资源
public static void release(Connection connection, Statement statement, ResultSet resultSet) throws SQLException {
if(resultSet!=null)
{
resultSet.close();
}
if(statement!=null)
{
statement.close();
}
if(connection!=null)
{
connection.close();
}
}
}
操作类
package mysql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
//dbcp池化技术
public class dbcp_5 {
public static void main(String[] args) throws SQLException {
Connection connection = utils_dbcp_5.connector();//使用第三方配置
Statement st = connection.createStatement();
ResultSet resultSet = null;
//插入
//String sql = "INSERT INTO users(`id`,`NAME`,`PASSWORD`,`email`,`birthday`)" + "VALUES(5,'grys','123456','44.qq.com','2001-3-13') ";
//删除
//String sql = "DELETE FROM `users` WHERE id = 5";
//改变
String sql = "UPDATE `users` SET `NAME` = 'star' WHERE id = 2";
int i = st.executeUpdate(sql);
if(i>0)
{
System.out.println("操作成功");
}
utils_dbcp_5.release(connection,st,resultSet);
}
}