1、数据库相关的操作命令:
- 登录: mysql -h locahost -u root -p 123456
- 创建数据库: create database 数据库名称
- 显示全部数据库: show databases;
- 删除数据库: drop database 数据库名称
2、表的相关操作命令:
- 创建表:create table 表名(字段名,类型(大小) Primary key auto_increment);
- 删除表:drop table 表名,表名,表名;
3、CURD(create update read delete)
- 添加: insert into 表名(字段,字段) values(值,值);
- 删除: delete from 表名 where id = "";
- 查询: select * from 表名;
- 修改: update 表名 set 字段 = "",字段 = “” where id = "";
4、异常抛出
new throw Exception();
5、JDBC:
步骤:
1、 导入数据库驱动 Class.forName("com.mysql.jdbc.Driver");
2、 创建数据库连接 Connection c = DriverManager.getConnection();
3、 获取数据库连接 Statement s = c.createStatement(sql) / PreparedStatement ps = c.preparedStatement(); //preparedStatement是statement的子类
4、 执行 ResultSet rs = s.execute() / ResultSet rs = ps.executeUpdate();
5、 遍历结果集
- 多条数据 - 一条数据
while(rs.next){ if(rs.next){
} }
6、创建用户名和密码的查询方法
public boolean login(String username, String password){
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){
e.printStackTrace();
}
String sql = "select * from user where username = ? and password = ?";
try(Connection c = DriverManager.getConnection("jdbc:mysql://localhost:3306/web01?characterEncoding=UTF-8","root","123456");
PreparedStatement ps = c.prepareStatement(sql)){
ps.setString(1,username);
ps.setString(2,password);
ResultSet rs = ps.executeUpdate();
if(rs.next()){
return true;
}else{
return false;
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
7、sql注入
如果使用Statement对象可能会出现问题
假设: sql = select * from user where username = ' " + username+ "' and password = '" + password +"';
传入的值是 ("cl,"kok' or '1' = '1"); 那就无论如何都成立。程序就存在漏洞
8、分页查询
String sql = "select * from user limit ?,?";
页数:pageNumber
每页显示条数: pageCount
例: 第8页 每页显示9条
计算: (8 -1) * 9 = 63
9、JDBCUtils
public Connection getConnection(){
try{
Class.forName("com.mysql.jdbc.Driver");
return DriverManager.getConnection();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
10、事务
Connection c = JDNCUtils.getConnection();
c.setAutoCommit(false); //开启事务
c.commit();
11、连接池(C3p0、dbcp)
C3p0:
步骤:
- 导入 jar包
- 在static代码块初始化
private final String URL = "jdbc:mysql://localhost:3306/web01?characterEncoding=UTF-8";
private final String USER = "root";
private final String PASSWORD = "123456";
static{
c3p0 = new ComboPooledDataSource();
c3p0.setDriverClass("com.mysql.jdbc.Driver");
c3p0.setJdbcUrl(URL);
c3p0.setUser(USER);
c3p0.setPassword(PASSWORD);
....其他配置
}
public Connection getConnection(){
try{
return c3p0.getConnection();
}catch(Exception e){
e.printStackTrace();
}
return null;
}
dbcp:
private final String URL = "jdbc:mysql://localhost:3306/web01?characterEncoding=UTF-8";
private final String USER = "root";
private final String PASSWORD = "123456";
static {
//初始化dbcp数据源
ds = new BasicDataSource();
ds.setDriverClassName("com.mysql.jdbc.Driver");
ds.setUrl(URL);
ds.setUsername(USER);
ds.setPassword(PASSWORD);
ds.setInitialSize(5); //初始化连接大小
ds.setMaxTotal(20);
ds.setMinIdle(3);
}
public static Connection getConnection() {
try {
return ds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
JDBC复习笔记
最新推荐文章于 2020-11-27 20:41:22 发布