欢迎访问我的blogThe Blog of Dxhua
数据库
DDL数据库定义语言
适用对象:数据库、表结构
功能:
进入数据库: mysql -u root -p
- 显示数据库列表----show databases;(注:MySql语句分割符为;)
- 建库—creat database yourDbName;
- 指定使用的数据库----use yourDbName(指定数据库)
- 显示库中的表----show tables;
- 建表-----create table yourTableName(columneName columneType,…)
- 更新表结构,删除表结构-------alter table yourTableName add column columneName columneType;(更新)-----alter table yourTableName drop column columneName;(删除)
- 删库和删表-----drop database yourDbName;(删库)--------------drop table yourTableName;(删表)
- 查询表的结构----desc yourTableName;
命名规则和数据类型
命名规则与java变量命名类似
- 数据类型
(1)整型(tinyint,smallint,mediumint,int(integer),bigint)
(2)浮点型(float和double)
(3)字符串(char,varchar,tinytext,text,mediumtext,longtext)前两个需要指定长度,后面的text不需要指定长度
(4)日起与时间类型(datetime,date,timestamp,time,year)
- date 日期“2018-09-09”
- time 时间“13:32:32”
- datatime 日期时间 “2018-09-09 13:32:32”
- timestamp 自动存储记录修改时间
(5)二进制数据(Blob) 存储二进制数据,用来存储大文件,多媒体文件
Sql语句详细见!(http://dxhua.top/2018/07/31/数据库-2017/)
多表查询
假如有两张表,user和user_info
user表:
±-----±---------±--------+
| id | account | info_id |
±-----±---------±--------+
| 1 | xiapming | 1 |
| 2 | xiaogang | 2 |
| 3 | dxhua | 3 |
| 4 | dxhua4 | 4 |
| 5 | xiaohua | 5 |
±-----±---------±--------+
user_info表:
±--------±--------±-----+
| info_id | name | age |
±--------±--------±-----+
| 1 | 小明 | 22 |
| 2 | 小刚 | 32 |
| 3 | 丁小华 | 22 |
| 4 | 丁小华4 | 22 |
| 8 | 小红 |18 |
±--------±--------±-----+ - inner join(内连接),将两个表连接起来,返回结果同时满足左右表连接的条件数据
格式 select * form 表名1 别名1 inner join 表名2 别名2 on 别名1.条件列=别名2.条件列;
例如把user表和user_info表连接
select * from user u inner jion user_info i on u.info_id=i.info_id;
结果 :
±-----±---------±--------±--------±--------±-----+
| id | account | info_id | info_id | name | age |
±-----±---------±--------±--------±--------±-----+
| 1 | xiapming | 1 | 1 | 小明 | 22 |
| 2 | xiaogang | 2 | 2 | 小刚 | 32 |
| 3 | dxhua | 3 | 3 | 丁小华 | 22 |
| 4 | dxhua4 | 4 | 4 | 丁小华4 | 22 |
±-----±---------±--------±--------±--------±-----+ - left join (左连接)返回结果除了内连接的结果外,还有左表中不符合条件的数据,并在右表相应的列上填上null
格式 select * from 表名1 别名1 left join 表名2 别名2 on 别名1.条件列=别名2.条件列
例子:
select * from user u left join user_info i on u.info_id=i.info_id;
结果:
±-----±---------±--------±--------±--------±-----+
| id | account | info_id | info_id | name | age |
±-----±---------±--------±--------±--------±-----+
| 1 | xiapming | 1 | 1 | 小明 | 22 |
| 2 | xiaogang | 2 | 2 | 小刚 | 32 |
| 3 | dxhua | 3 | 3 | 丁小华 | 22 |
| 4 | dxhua4 | 4 | 4 | 丁小华4 | 22 |
| 5 | xiaohua | 5 | NULL | NULL | NULL |
±-----±---------±--------±--------±--------±-----+ - right join(右连接)返回结果除了内连接的结果外还有右表中不符合条件的数据,并在左表相应的列上填上null
格式 select * from 表名1 别名1 right join 表名2 别名2 on 别名1.条件列=别名2.条件列
例子:
select * from user u right join user_info i on u.info_id=i.info_id;
结果:
±-----±---------±--------±--------±--------±-----+
| id | account | info_id | info_id | name | age |
±-----±---------±--------±--------±--------±-----+
| 1 | xiapming | 1 | 1 | 小明 | 22 |
| 2 | xiaogang | 2 | 2 | 小刚 | 32 |
| 3 | dxhua | 3 | 3 | 丁小华 | 22 |
| 4 | dxhua4 | 4 | 4 | 丁小华4 | 22 |
| NULL | NULL | NULL | 8 | 小红 | 18 |
±-----±---------±--------±--------±--------±-----+ - full join (全连接)返回结果除了内连接的结果外还有两个表中不符合条件的数据,并在左右表相应的列上填上null (mysql不支持,很少使用)
使用navicat可视化操作数据库
设计数据库 电商网站表
JDBC的应用
下载jar包后,连接数据库 驱动下载地址!(https://dev.mysql.com/downloads/file/?id=470333)
public class JDBC {
public static void main(String [] args) throws SQLException, ClassNotFoundException {
//1.注册数据库驱动
// Driver driver = new com.mysql.jdbc.Driver();
// DriverManager.registerDriver(driver);//因为com.mysql.jdbc.Driver类中已经注册了,不这样用,用反射的方法直接拿到这个类就行了
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库
// String url = "jdbc:mysql://localhost:3306/mail";//dbc:mysql://localhost:3306/数据库名
// Connection connection = DriverManager.getConnection(url, "root", "root");//数据库用户名、密码
// connection.close();
Connection connection = getConnection();
connection.close();
}
private static Connection getConnection() throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库
String url = "dbc:mysql://localhost:3306/mail";
return DriverManager.getConnection(url,"root","ding123456");
}
}
JDBC查询数据库
public class query {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
//1.注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库
String url = "jdbc:mysql://localhost:3306/mail";
Connection connection = DriverManager.getConnection(url, "root", "ding123456");
//3.获取操作数据库的对象
Statement statement = connection.createStatement();
String sql = "select * from production";
// 4.遍历结果集
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()){
String p_name = resultSet.getString("p_name");
BigDecimal p_price = resultSet.getBigDecimal("p_price");
System.out.println("商品名称:"+p_name+"商品价格"+p_price);
}
connection.close();
}
}
其中插入,更新,删除都使用statement.executeUudate(sql语句)方法
//1.注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库
String url = "jdbc:mysql://localhost:3306/mail";
Connection connection = DriverManager.getConnection(url, "root", "ding123456");
//获取操作数据库的对象
Statement statement = connection.createStatement();
// String sql = "insert into production values (null,'玩具枪','100','http',2)";
// String sql = "update production set p_price=80 where p_id=4";
String sql = "delete from production where p_id=4";
// 遍历结果集
int resultSet = statement.executeUpdate(sql);
System.out.println(resultSet);
statement.close();
connection.close();
JUnit4用来测试方法,首先可以创建一个test文件夹,将文件夹变成source文件夹才可以创建类 创建test类,创建test方法,在test方法上加上@Test,方法二将test问价加变成test型的文件夹,在呀测试的方法处Ctrl+shift+t可以直接测试
改变文件夹属性在project Structure -->Modules—>Sources—>点击test,然后点击上面的mark as:的选项
防sql注入使用Statement的子类prepareStatement来预编译,
比如一个简单的登录
login没有防SQL注入,login1采用prepareStatement有防SQL注入
sql注入语句可以是 a’ or ‘a’='a
public class Login {
public void login(String account,String password) throws ClassNotFoundException, SQLException {
//1.注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库
String url = "jdbc:mysql://localhost:3306/mail";
Connection connection = DriverManager.getConnection(url, "root", "ding123456");
//获取操作数据库的对象
Statement statement = connection.createStatement();
String sql = "select * from user where account='"+account+"' and password='"+password+"' ";
// 遍历结果集
ResultSet resultSet = statement.executeQuery(sql);
if (resultSet.next()){
String nickname = resultSet.getString("nickname");
System.out.println(nickname+"登录成功");
}else {
System.out.println("登录失败");
}
statement.close();
resultSet.close();
connection.close();
}
//防sql注入使用Statement的子类prepareStatement来预编译,
public void login1(String account,String password) throws ClassNotFoundException, SQLException {
//1.注册数据库驱动
Class.forName("com.mysql.jdbc.Driver");
//2.获取数据库
String url = "jdbc:mysql://localhost:3306/mail";
Connection connection = DriverManager.getConnection(url, "root", "ding123456");
//获取操作数据库的对象
// Statement statement = connection.createStatement();
String sql = "select * from user where account=? and password=?";
// 遍历结果集
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,account);//前面数字为索引,表示第几个问号,从1开始
preparedStatement.setString(2,password);
ResultSet resultSet = preparedStatement.executeQuery();
// ResultSet resultSet = statement.executeQuery(sql);
if (resultSet.next()){
String nickname = resultSet.getString("nickname");
System.out.println(nickname+"登录成功");
}else {
System.out.println("登录失败");
}
preparedStatement.close();
resultSet.close();
connection.close();
}
}