自用
主要针对数据库实训总结常用的MySQL命令
和JDBC相关操作
MySQL数据库操作
一、数据库、数据表
-
create database university
-
use university
-
show university
-
drop university
-
create table classroom(
ID int not null,
name varvhar(20),
primary key(ID, name)
constraint ‘fk’ foreign key(‘ID’) references another_table(‘ID’)
); -
alter table classroom rename new_classroom;
-
alter table classroom change ID new_ID varchar(20);
-
alter table classroom change ID ID varchar(40);
-
alter table classroom add new_col int first/after ID;
-
alter table classroom drop ID
-
drop table classroom
-
describe classroom
查看表中数据等
-
show create table classroom
第一次用,试试,可以查看表的属性
二、查、增删改,索引、视图
唔,想起搜索功能实现的时候可以用查询的匹配条件嘞
提升性能也许可以尝试索引,
然后为了存储方便可以弄个触发器自动增减记录中的某字段
- Query
- select * from A
- select ID, name from A order by ID DESC/ASC
- select ID from A where ID in (‘01’,‘02’)
- select ID from A where ID between ‘01’ and ‘05’
- select name from A where name like ‘%a%’(%匹配任意串)
- select distinct name from A where name like ‘a’ (_匹配单个字符)
- select * from A where ID is not null
- 分页
select * from A limit 20,20 (从20行开始,向后查询20行,常常用于处理页面值与每页记录条数的关系) - select ID, max(name) from A group by ID (聚集函数,记得使用Group by)
-
Insert
insert into A(ID, name) values(‘06’,‘test’)
insert into A (ID, name) values (‘1’,‘a’), (‘2’,‘b’), (‘3’,‘c’); (多条记录)
insert into A(ID) values(‘06’); -
Delete
delete from A where ID = ‘06’;
delete from A (删除所有记录,慎) -
Update
update A set name = ‘new_name’ where ID =‘06’ -
索引
create table A(ID int , unique index idx_ID(ID))
create index idx_ID on A (ID);
alter table A add index idx_ID (ID);
drop index idx_ID on A -
视图
create view v1 as(select语句)
drop view v1 if exists v1 -
触发器
create trigger tri_g
after insert on A
for each row
update Book set num = num+1 where new.ID = Book.IDdelimiter && (更改结束符)
&& delimiter ;
show triggers
drop trigger trig_g
JDBC
显然,也许咕咕了
Java Data Base Connectivity
一、连接数据库
- 报错 Could not create connection to database server.
是由于版本问题。
阿,sl,搞了一个小时才弄好,这一切的起因是因为想不使用jsp项目调试jdbc,所以新建了一个java项目。
原来是版本问题,好lay(JDBC包与SQL版本要一致,命令略有差异)
新版本是“com.mysql.cj.jdbc.Driver”,且要加上时区的声明
旧版没有cj,无需声明时区。
- 学到了,用Class.forName要在try语句里面。
import java.sql.*;
public class test {
public static void main(String [] args){
String jdbcName="com.mysql.cj.jdbc.Driver";
String dbUrl ="jdbc:mysql://localhost:3306/product?serverTimezone=UTC";
//登录信息
String dbUser = "root";
Connection con = null;
String dbPassword = "";
System.out.println("Hi IDEA");
try{
//加载数据库驱动
Class.forName(jdbcName).newInstance();
System.out.println("OK");
}
catch (Exception e){
e.printStackTrace();
}
try{
//建立数据库连接
con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
System.out.println("connected");
}
catch (SQLException e){
e.printStackTrace();
System.out.println("Failed");
}
finally {
try {
con.close();
System.out.println("Closed");
}
catch (SQLException e){
e.printStackTrace();
System.out.println("Close failed");
}
}
}
}
因为版本的原因,又过去了1h…心好lay
- DriverManager驱动管理类
-
Class.forName(jdbcName).newInstance();
-
Connection con = DriverManager.getConnection(dbUrl,dbUser,dbPassword);
-
con.close();
二、增删查改
都是差不多,PrepareStatement用的比较多,可以预编译,后续设置参数,执行速度也比较快。
增删查改基本不变,变的是sql里的内容,按照sql语法来即可。
PrepareStatement & Statement
//首先要连接
Connection conn = Dbutil.getCon();
String sql = "update test_vegetables set productID=?, productName=?,salerName=?,days=?,quantity=? where productID = ?";
//Preparement语句编译
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1,vegatables.getProductID());
pstmt.setString(2,vegatables.getProductName());
pstmt.setString(3,vegatables.getSalerName());
pstmt.setInt(4,vegatables.getDays());
pstmt.setInt(5,vegatables.getQuantity());
//preparement语句执行更新
int res = pstmt.executeUpdate();
//关闭连接
dbutil.close(pstmt,conn);
statement执行时executeUpdate里要放参数sql
Statement stmt = con.createStatement();
int res = stmt.executeUpdate(sql);
if(res == 1){
System.out.println("数据库插入成功");
}
else{
System.out.println("数据库插入失败");
}
总结:
PreparementStatement继承了Statement,支持Statement的基本功能
同时还能动态处理数据
PreparementStatement:
pstmt = con.prepareStatement (sql)
pstmt.excuteUpdate()
Statement
stmt = con.createStatement()
stmt.executeUpdate()