zoukankan      html  css  js  c++  java
  • 【幻化万千戏红尘】qianfengDay24-java基础学习:数据库、七大表语句、JDBC反射

    课程回顾:

    网络
    IP
    端口
    TCP/IP协议
    TCP的特点:
    1、面向连接
    2、每次传输大小无限制

    使用TCP的步骤:
    1、创建客户端(Socket)或服务端(ServerSocket)
    2、获取需要的流,声明要操作的变量
    3、操作数据(从流中加载数据,将数据写入到流中)
    4、关闭


    今日内容:
    数据库:存储数据的仓库
    稳定、安全、性能高
    SQLite:是一种轻量级的嵌入式的数据库
    是C语言开发的,免费数据库
    2000是第一个版本
    具有良好的SQL解释器、数据库引擎等
    特点:
    1、轻量级
    2、性能良好
    3、零成本
    应用范围很广泛不仅Android而且IOS等也在使用

    SQL:结构化查询语言Structured Query Language
    Lite:简化的

    数据库:有不同的表组成,其中表名唯一
    表:有字段和数据行组成
    同一表中,字段名唯一
    SQL:结构化查询语言
    是用来操作数据库的语言
    不区分大小写

    数据库的数据类型:
    整型:intINTEGER umber
    浮点型:floatdouble
    字符串:varchar varchar ext
    二进制:blob

    SQLite没有数据类型,但是推荐大家写上类型


    软件的命令:以.开头,没有;
    .help:打开软件支持的命令
    .open:打开指定的数据库文件,存在就打开,不存在就创建
    .tables:显示数据库中所有的表名
    .schema:显示建表语句
    .output:重定向 .output stdout:重新设置会软件
    .import:导入 .import 文件名.后缀 表名

    SQL语句:没有.开头,但是必须有;结束
    1、建表语句
    格式:CREATE TABLE <表名> (字段名称 [数据类型] [约束条件],……);

    约束条件:
    primary key:主键,表中数据唯一,不可重复
    not null:非空,必须得有值
    default:默认值
    //创建学生表
    create table Student (id INTEGER primary key,name varchar(10) not null,sex varchar(2));

    2、修改表语句
    格式:ALTER TABLE <表名> ADD 字段名称 [数据类型];
    注:alter语句在SQLite数据库中只支持add

    //在Student表中新增字段
    alter table Student add age int ;


    3、删除表语句
    格式:DROP TABLE <表名> ;
    //删除表结构
    drop table Student;


    4、添加语句
    格式:INSERT INTO <表名>(字段名称,……) VALUES(值1,值2,……);
    字符串数据记得要加双引号
    //新增一名学生,完整写法
    insert into Student(id,name,sex) values(16060001,"周忠箱","靓仔");
    insert into Student(id,name,sex,age) values(16060020,"昝争强","靓仔",13);
    insert into Student(id,name,sex,age) values(16060032,"陈宝田","靓仔",12);
    insert into Student(id,name,sex,age) values(16060088,"廖伟凯","靓仔",11);
    insert into Student(id,name,sex,age) values(16060013,"胡媚","靓女",10);
    insert into Student(id,name,sex,age) values(16060018,"蒋成成","靓仔",17);
    insert into Student(id,name,sex,age) values(16060009,"韦功成","靓仔",15);
    insert into Student(sex,id,name) values("男",16060010,"严阳");
    //添加学生,简写形式
    insert into Student values(16060003,"朱小凤","靓女");
    //查询
    select * from Student;
    5、修改语句
    格式:UPDATE <表名> SET 字段名称=值,…… [WHERE 条件];

    //修改学生的信息
    update Student set sex="靓仔" where id=16060010;
    update Student set age=18;

    6、删除语句
    格式:DELETE FROM <表名> [WHERE 条件];

    //删除学号为16060001的学生
    delete from Student where id=16060001;

    7、查询语句
    格式:SELECT [字段1,字段2,……][*] FROM <表名> [WHERE 条件] [ORDER BY 字段 ASC|DESC] [GROUP BY 字段];

    查询涉及:
    *:表示所有字段
    where:条件,只要符合条件就操作
    ><=!=<>:关系,大于,小于,等于,不等于,不等于
    and:且
    or:或者
    in (值,……):在指定的值之中
    between A and B:在A和B之间
    limit index,count:分页查找,从index索引行开始,查询count条数据
    order by 字段 ASC|DESC:按照字段进行排序,ASC:升序(默认),DESC:降序
    group by 字段:按照指定的字段进行分组
    like:模糊查询
    %:任意个字符
    _:一个字符
    COUNT:计数,计算指定字段不为null的行数
    SUM:求和
    MAX:最大
    MIN:最小
    AVG:平均


    //查询Student表中所有字段的值
    select * from Student;
    和下面的语句等同
    select id,name,sex,age from Student;
    //查询学号为16060003的学生
    select * from Student where id=16060003;
    //大于16060003的学生
    select * from Student where id>16060003;
    //查询学号不等于16060010且性别为女的学生
    select * from Student where id <>16060010 and sex="靓女";
    //查询学号大于16060020或者年龄小于15的学生
    select * from Student where id>16060020 or age<15;
    //查询年龄为12、15、18的学生
    select * from Student where age=18 or age=15 or age=12;
    select * from Student where age in (12,15,18);
    //查询年龄为10到20之间的学生
    select * from Student where age >=10 and age<=20;
    select * from Student where age between 10 and 20;

    分页查找:limit
    为了提高性能
    每页显示3条,查询第1页的数据
    select * from Student limit 0,3;
    每页显示3条,查询第2页的数据
    select * from Student limit 3,3;

    子查询:
    修改第5条数据的年龄为22
    update Student set age=22 where id=(select id from Student limit 4,1);

    //查询学生按照年龄升序排列
    select * from Student order by age;
    等同于下面语句
    select * from Student order by age asc;
    //查询学生,按照年龄升序排列,年龄相同再按照学号降序排列
    select * from Student order by age asc,id DESC;
    //查询学生,按照学号进行降序排列
    select * from Student order by id DESC;

    聚合函数:
    COUNT:计数,计算指定字段不为null的行数
    SUM:求和
    MAX:最大
    MIN:最小
    AVG:平均
    //查询Student表的数据
    select COUNT(id) from Student;
    select COUNT(1) from Student;
    select COUNT(*) from Student;
    //查询所有年龄的总和
    select SUM(age) from Student;
    select MAX(age),MIN(age),AVG(age) from Student;
    select date();//显示日期
    select datetime();//显示时间

    //查询每种性别的人数
    select COUNT(id),sex from Student group by sex;
    select COUNT(id),age from Student group by age;

    模糊查询:like
    %:任意个字符[0,n];
    _:1个字符
    //查询学号以2结尾的
    select * from Student where id like "%2";
    select * from Student where id like "_2";//2个字符
    //查询年龄以1开头的2位数的学生
    select * from Student where age like "1_";
    //切记:SQLite数据库不支持中文的模糊查找
    select * from Student where name like "李%";
    //查询学号包含2的学生
    select * from Student where id like "%2%";

    A%:以A开头
    %A:以A结尾
    %A%:包含A
    A_:以A开头,后面有且只有一个字符:2
    _A:以A结尾,且前面有且只有一个字符:2
    _A_:包含A,且前后各有一个字符:3

    练习:打开任意一数据库
    创建表
    News新闻
    字段:id序号 itle标题content正文 ime时间comment评论 ype类型author编辑
    1)、按照要求创建表
    create table News (id INTEGER primary key,title varchar(20),content text,time varchar(20),comment int,type varchar(2),author varchar(10));
    2)、新增10条新闻
    insert into News(title,content,time,comment,type,author)values("计划失败,吃货兼网虫的悲哀","为了让自己多活动,我把放在电脑桌上的零食拿到了外面的茶几上,这样最起码为了吃我也能走动走动。…………现在我的零食经常会过期……","2015年11月12日 08:20:10",10,"笑话","爆料大王");
    insert into News(title,content,time,comment,type,author)values("吃货兼网虫的悲哀","为了让自己多活动,我把放在电脑桌上的零食拿到了外面的茶几上","2016年11月12日 08:20:10",10,"笑话","爆料小王");
    insert into News(title,content,time,comment,type,author)values("内心是崩溃的","在家叫我滚出去,在外面喊我滚回家,不补课说我学习差,补课说我浪费钱,吃东西说我嘴巴馋,不吃说我要成仙,在家不学习说我不努力,在家一学习说我只会玩手机,不讲话说我闷得很,讲话说我屁话多,到底想让人怎么样。同意的赞一个。","2016年1月12日 08:20:10",10,"笑话","爆料大王");
    insert into News(title,content,time,comment,type,author)values("悲哀","电脑桌上的零食拿到了外面的茶几上","2016年03月12日 08:20:10",10,"笑话","爆料大王");
    insert into News(title,content,time,comment,type,author)values("网虫","为了让自己多活动……","2015年12月12日 08:20:10",10,"时事","爆料大王");
    insert into News(title,content,time,comment,type,author)values("失败","……","2016年04月12日 08:20:10",10,"娱乐","爆料大王");
    insert into News(title,content,time,comment,type,author)values("叔叔救我","喊我滚回家,不补课说我学习差,补课说我浪费钱,吃东西说我嘴巴馋,不吃说我要成仙,在家不学习说我不努力,在家一学习说我只会玩手机,不讲话说我闷得很,讲话说我屁话多,到底想让人怎么样。同意的赞一个。","2016年06月12日 08:20:10",10,"热点","小鲜肉");
    insert into News(title,content,time,comment,type,author)values("悲哀","电脑桌上的零食拿到了外面的茶几上","2016年03月12日 08:20:10",10,"笑话","爆料王");
    insert into News(title,content,time,comment,type,author)values("测试","为了让自己多活动……","2015年12月11日 08:20:10",10,"时事","大王");
    insert into News(title,content,time,comment,type,author)values("看看","……","2016年06月12日 08:20:10",10,"娱乐","王");
    3)、查询按照时间进行降序排列
    select * from News order by time DESC;
    4)、修改第3条的author="百科"
    update News set author="百科" where id=(select id from News limit 2,1);
    5)、删除评论数为0且类型为时事的新闻
    delete from News where comment=0 and type="时事";
    6)、查询每种类型的新闻数量
    select type,COUNT(id) from News group by type;
    7)、分页查询,每页显示4条数据,请查询第3页的数据
    select * from News limit 8,4;


    练习:请创建数据库Test.db
    创建表Phone,字段自定义,要求必须有主键
    新增5条数据


    反射:在运行时获取指定类的属性、方法、构造器的过程就称为反射机制
    反射的用途:
    1、获取指定类的属性、方法、构造器
    2、执行私有属性或方法

    Class<T>:反射的体现,对应的字节码的描述
    基本数据类型也可以使用class
    可以获取对应的类的属性、构造方法、方法等内容
    Class类的对象创建的方式:
    1、forName
    2、类名.class
    3、对象.getClass

    常用方法:

    JDBC:java语言操作数据库的API

    各个数据库厂商自己实现属于的自己的驱动

    JDBC使用步骤:
    1、加载驱动
    2、获取连接对象
    3、获取操作SQL对象
    4、获取查询结果
    5、关闭

  • 相关阅读:
    C# 单元测试
    支持库:DateTime扩展
    根据枚举名称创建枚举
    支持库:String扩展
    数组的几道面试题转
    java推荐书籍及下载
    Python天天美味(总) 转
    python 实现文件的递归拷贝转
    关于python文件操作转
    JDK源码分析收藏地址
  • 原文地址:https://www.cnblogs.com/weigongcheng/p/5763654.html
Copyright © 2011-2022 走看看