java基础知识2

1.转发(forward)和重定向(redirect)的区别?

      1).效率上

                 转发(forward) > 重定向(redirect)

      2).显示上

                  重定向(redirect): 显示新的URL

                  转发(forward): 地址栏不变

      3).数据上

                  转发(forward): 可以共享request里面的数据 

                  重定向(redirect): 不能

     4).请求次数

                 重定向(redirect): 两次

                 转发(forward):     一次

       重定向是客户端行为,转发是服务器行为

       重定向可以访问自己的web应用以外的资源,在重定向过程中传输的信息会丢失.

       请求转发是服务器内部把一个request/response的处理权移交给另一个,对客户而言,只知道最早的请求A,而不知道B,甚至C,D. 传输的信息不会丢失

2.CSS选择器类型?

      1)标签选择器(如: body  div  p  ul  li)

                用户点击按钮之后,所有<p> 元素全都隐藏

          $(document).ready(function(){ 

              $("button").click(function{

                     $("p").hide();

               });

         });

    2)  #id选择器

              当用户点击按钮之后,有  id = "test" 属性的元素将被隐藏

         $(document).ready(function(){

              $("button").click(function(){

                  $("#test").hide();

               });

           });

     3) .class选择器

    用户点击按钮之后带有  class = "test" 属性的元素隐藏

         $(document).ready(function(){

                 $("button").click(function(){

                     $(".test").hide();

               });

           });

3.  HTML和JSP的区别?

      HTML: 静态页面

                 不需要指定编译工具,只需要在TXT文档中写上HTML标记就就行

       JSP: 动态页面

               需要经过JDK编译之后后把内容发给客户端去显示

               把JSP转译成一个servlet文件,然后在编译成class文件,

               当用户访问JSP文件时就执行class文件.

区别:

      1.HTML能直接打开,JSP只能发布到Tomact等服务器上才能打开

      2.HTML页面是静态页面,可以直接运行,

          JSP页面是动态页面它运行时需要转化为Servlet

     3.JSP在表头中有编码格式和导入包等

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
 <!-- 导入spring标签库 -->
 <%@taglib prefix="form" uri="http://www.springframework.org/tags/form" %>

     4.JSP<%%>就可以写java代码了,而HTML没有<%%>

4.Mysql和Oracle的区别?

       1.Oracle是大型数据库而Mysql是小型数据库,

          Oracle的市场占有率达40%,Mysql只有20%左右.

          Mysql是开源的,Oracle价格非常高

      2.Oracle支持大并发,大访问量

      3.安装所用的空间差别很大:

        Mysql安装完后才152M,而Oracle有3G左右,

        且使用Oracle占用特别大的内存空间和其它机器性能

     4.Mysql和Oracle在操作上的区别:

        (1)创建表:

             Mysql有 create table if not exist 方式创建表,

             对于在程序中自动实现表创建的情况很方便

            Oracle不支持if not exist 和 drop table if exists 语句

        (2)主键

            Mysql一般使用自动增长类型,在创建表时,只需指定表的主键为auto_increment,

            插入记录时,不需要指定该记录的主键值.Mysql将自动增长.  

            Oracle不能使用auto_increment

            Oracle的主键一般默认是4个字节,Mysql没有要求

       (3)单引号的处理

              Mysql可以使用双引号包起字符串

              Oracle只能用单引号包起字符串

     (4)Mysql和Oracle分页处理

        Oracle分页:

          Oracle分页标准计算公式:  page: 当前页码,    pageSize: 每页显示个数

          select * from (select rownum rn, zhou.*from zhou) 

       where rn between (page-1)*pagePage + 1 and page*pageSize;

       Oracle分页注意事项:

       在使用rownum时,不能对rownum进行大于0的某一数的判断,

        因为rownum必须从0开始递增

     ---查询第三条以后的记录

        错误的:select rownum,id,name ,salary from zhou where rownum > 3; 

        正确的: select * from (select rownum rn,zhou.* from zhou) where rn >3;

     Mysql分页查询:

           select * from 表名 limit 起始行号,行数

        起始的行号从0开始,如果和order by连用,order by 在前

   ---列出总成绩并且按照降序排列,只打印三行

select name,sum(score) from score group by name order by sum(score) desc limit 3;

     (5) 字符串的模糊查询

         Mysql:

                select * from 表名 where 字段名 like '%条件%';

                 %C: 以C结尾.   C%: 以C开头

         Oracle: 不能使用索引,速度不快

                     ---查询名字中第二个字母是A的员工信息

                    select id,name from zhou where name like '_A%';

                      %:任意0到多个字符,   _:任意一个字符

5.Servlet声明周期?

          通过调用 init( ) 方法进行初始化

          调用 service( ) 方法来处理客户端的请求

          通过调用 destory( ) 方法终止(结束)

          Servlet是由 JVM的垃圾回收器进行垃圾回收的

6.Resful风格?

          Resful: 符合REST约束风格和原则的应用程序或设计

         Http的请求动作一般分为四种:

              1) POST增加资源

              2)GET获取资源

              3)PUT修改资源

              4)DELETE删除资源

     前端的请求参数只能是GET或者POST,所以需要将POST请求转化为PUT/DELETE.

       通过此过滤器HiddenHttpMethodFilter进行转换,在前端发起请求时,

    添加一个_method将,此参数的值指定为请求类型PUT或者DELETE

    PUT/DELETE不能设置一级路径


@Controller
public class SecondController {
  @RequestMapping(value="/testGET/{name}", method=RequestMethod.GET) 
   public String testGET(@PathVariable("name") String name){
        System.out.println("查询某个学生的信息");
        return "hello";
    }
   @RequestMapping(value="/testPOST",method=RequestMethod.POST)
    public String testPOST(){
        System.out.println("添加某个学生");
        return "hello";
    }
    @RequestMapping(value="/testPUT",method=RequestMethod.PUT)
    public String testPUT(){
        System.out.println("修改某个学生的信息");
        return "redirect:hello.jsp";
    }
    @RequestMapping(value="/testDELETE",method=RequestMethod.DELETE)
    public String testDELETE(){
        System.out.println("删除某个学生的信息");
        return "redirect:hello.jsp";
    }
  }
 
//(2) hello.jsp
 <h1>hello</h1>

//(3)index.jsp
<h1>REST风格架构</h1>
<form action="testGET/李白" method="get">
        <input type="submit" value="get">
</form>
<br>
<form action="testPOST" method="post">
    <input type="submit" value="post">
</form>
<br>
<form action="testPUT" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="put">
</form>
<br>
<form action="testDELETE" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="submit" value="delete">
</form> 

7. 存储过程?

       数据库系统中一组提前编译的PL/SQL语句程序语句集,其中可以封装一些通用操作,

当我们需要执行这些通用操作时,可以通过制定存储过程名和参数名来调用储存过程

   作用:

           1.只需编译一次,以后每次执行不需要重复编译

           2.可以对数据一些通用复杂操作进行封装

           3.可以反复调用,减少工作量

    存储过程实例:

            传入员工id,给指定员工涨100元薪水,并且打印涨前和涨后薪水

     set serveroutput on;

     create or replace procedure raisesalary(empid IN number)

      AS

    --定义变量保存涨前和员工姓名

     pname zhou.name%type;

     psal zhou.salary%type;

     begin

   --获取员工的涨前薪水,赋值给变量psal

   select name,salary INTO pname,psal from zhou where id = empid;

  --将指定员工薪资涨100

 update zhou SET salary = salary + 100 where id = empid;

  --打印涨前和涨后薪水

 dbms_output.put_line('id:' || empid || ',' || 'name:'  || pname || ',' || '涨前:' || psal || ',' || '涨后:' || (psal + 100) );

 end;

 /

--调用存储过程

begin

raisesalary(1002);

raisesalay(1003);

end;

/

8. 触发器? 触发器的使用场景?

  概念: (可以看成是监听器)

         数据库中一个用于表相关的,存储一段PL/SQL程序,主要作用是用监听表中DML操作,

         当指定表上执行了insert  update  delete这些操作的语句时,

         就会自动触发存储在触发器中的PL/SQL语句

触发器类型

      (1)语句集触发器(针对表)

       在指定操作语句执行之前或之后会执行一次触发器中的PL/SQL,无论这个操作影响了多少数据(一般是insert  delete)

     (2)行级触发器(针对行)

       触发器语句作用的每一行记录都会备被触发,可以使用

     :new和  :old两个变量来记录这一行数据被影响之前和之后的状态(一般是update)

     (3)创建触发器语法

 create[or replace] trigger trigger_name(触发器名)

               {berore | after} 

       {insert | delete | update[of column]}

         on 表名

         [for each row [where(条件)]]

                 declare

                  begin

                        PL/SQL程序

                  end;

                    /

      (4)触发器的使用场景:

             1.复杂的安全性检查

         ---禁止在非工作时间往数据表中插入数据(上午9点之前,下午18点之后)

               create or replace trigger trigger_securityemp

               before

               insert on myemp

              declare

              begin

                  --判断当前时间如果不是在9点到18点之间抛出异常,禁止插入

               if extract(hour from systimestamp) + 8

               not between 9 and 18 then raise_application_error(-20001,'禁止在非工作时间插入数据');

               end if;

               end;

                 /

              insert into myemp (name, salary,obj) values ('碧瑶',6000,'销售');

             2.数据的确定

             3.数据库审计

             4.数据的备份和同步

9.联查

     inner join:

              等值连接,只返回两个表中连接字段相等的行

                 select chinese.name from chinese inner join math on chinese.name = math.name;

      left join:

              返回包括左表中的所有记录和右表中连接字段相等的记录

               select chinese.name,chinese.job,math.obj,math.score from chinese left join math on chinese.name = math.name;

     right join:

             返回包括右表中所有记录和左表中连接字段相等的记录

     union去重:

             union不会去重,union可以连接多个查询语句,每个表的结构必须一致

                        select name from chinese union select name from math;

10.聚合函数, 分组

      聚合函数:

            1)平均成绩: as:给字段起别名

                select avg(age) as '平均成绩' from student;   

            2)最大年龄

                select max(age) as '最大年龄' from student;

           3) select * from student where age = (select max(age) from student);

           4)找出比平均年龄小的

               select * from student where age < (select avg(age) from student);

           5)该字段的记录数

               select count(sid) from student;

          6)张三的平均成绩

              select avg(score) from score where name = '张三';

       分组:

             select 字段名 聚合函数 from 表名 group by 字段名[having 筛选条件]

                 注意:

                       1.前面已经执行完了,才会执行having

                       2.select 后面出现的字段名必须是 group by 后面的字段名

      1)列出每个人的平均成绩和总成绩

              select name,avg(score) as '平均成绩',sum(score) as '总成绩' from score group by name;

     2)列出平均成绩大于70的人名和成绩

             select name,avg(score) as '平均成绩' from score group by name having avg(acore) > 70;

     3)列出平均成绩 > 70 总成绩 > 180的人

            select name,avg(acore) as '平均成绩', sum(score) as '总成绩' from score

              group by name having avg(score) > 70 or sum(score) > 180;

    4)列出总成绩并且按照升序排列     desc 降序

               select name,sum(score) from score group by name order by sum(score);

    5)列出每科的平均成绩

               select obj,avg(score) as '平均成绩' from score group by obj;

    6)列出总成绩并且按照降序排列,并且只打印3行

              select name,sum(score) from score group by name order by sum(score) desc limit 3;

11.外键

              (1)作用: 可以为两张表后者多张表建立关联,保证了数据之间的一致性,完整性等

                            一个表的外键肯定是另一张表的主键

                           主键所在的表称之为主表,外键所在的表称之为从表

                            一张表虽然只有一个主键,但可以有多个外键

                           目前只有mysql引擎为innoDB时才支持外键(默认是innoDB)

           (2)外键的约束模式:

                 1.set null: 闲置模式

                         主表记录被删除或者更改,从表相关记录的外键置为null;

                2.cascade: 级联操作

                        主表中删除或者更新了某条信息,从表中与该表记录有关的记录也发生改变

                3.district: 严格模式    no action: 和district一样

                        当从表中有外键数据和主表关联,主表中该条记录就不能删除或者更新

                        (父表不能删除或者更新一个被子表引用的记录)

          (3)添加外键:  

                             constraint: 约束  

                             references: 参考

                            foreign key:外键约束

                            cascade: 主变从变   set null: 主变从为空(null)

                   alter table st add constraint te_fy_st 

                   foreign key(tid) references te(tid) 

                   on delete set null on update set null;

        (4)删除外键

                  alter table st drop foreign key te_fy_st;

12.索引

         (1)作用: 

               当某个表中某个字段经常作为查询条件(where 后面),并且表中有大量的数据,

        该表经常作为查询条件,这时就可以将该字段作为索引,提高查询效率,

        但是降低了增删改的效率

        (2)索引的创建

             2.1单例索引: 基于表中某一经常查询列来创建索引

              create [unique] index 索引名 on 表名(字段名);    unique:创建唯一索引

            2.2复合索引:

            基于多列来创建的索引,比如经常需要根据某两列来进行order by排序时会使用到

         ---基于myemp表的deptno salary 两列来创建复合索引

          create index idx_myemp_deptno_salary on myemp(depton,salary);

       ---在执行以下查询时,会自动调用上面的索引

         保证order by 列的顺序和创建索引时,列的顺序一致,复合索引才会起作用

         select id,name,deptno,salary from myemp order by deptno ASC, salary DESC;

       (3)索引的重建和删除

            3.1更新索引

                 如果表的索引列上经常执行DML操作,就需要对索引执行更新重建

                    语法: atler index 索引名 REBULLD;

          3.2删除索引

                索引表中索引类列中有不合理的索引,会导致操作性能下降

                    语法: drop index 索引名

      (4)索引的使用场景:

           1.为经常出现在where子句中列建立索引

           2.为经常出现在order by, distinct(关键字去重)后面的列建立索引

              如果是复合索引,索引后面的列顺序要和这些关键字后面列顺序一致

          3.为经常出现做表连接的连接条件列上建立索引

          4.不要在经常DML操作的列上建立索引

          5.不要在数据量少的表上建立索引

          6.限制表的索引数目,索引不是越多越好

          7.删除很少被使用,不合理的索引

13.MySql常用的?

    DDL(数据库定义语言)

       1.创建数据库:

           create database if not exists 库名 character set utf8 collate utf8_general_ci;

       2.创建表:

           create table if not exists 表名

            (字段名 字段类型 字段约束,字段名 字段类型 字段约束....) character set utf8;

       3.增加列:

                alter table 表名 add (列1,列2.....)

       4.修改列:

           4.1修改字段名及其类型:

                alter table 表名 change 旧列名 新列名 新列名类型

           4.2修改字段类型

                alter table 表名 modify 列名 新数据类型

     5.删除列: 

               alter table 表名 drop 列名,列名.....;

     6.修改表名

               rename table 旧表名 to 新表名

    7.复制表结构

              create table if not exists 新表名 like 旧表名;

  DML:

     1.增加行:

           insert into 表名(字段1,字段2....) values (值1,值2....)....

     2.删除行:

           delete from 表名 where 字段名 in (值1,值2....);

           delete from 表名 [where 字段名 = 值1,字段名 = 值2];

   3.修改行:

           update 表名 set 字段名 = 值1, 字段名 = 值2....[where id = ?];

  DQL:

        1.select 字段1,字段2......from 表名 [where 检索条件];

        2.select * from 表名

        3.按照某个字段排序:

                select * from 表名 order by 字段名[asc/desc];

       4.模糊查询: %C:以C结尾,  C%: 以C开头

               select * from  表名 where 字段名 like '%条件%';

       5.分页查询:

              select * from 表名 limit 起始行号,行数;

              起始的行号从0开始,如果和order by 连用,order by在前

      6.聚合函数

                avg(字段名): 该字段的平均数

                max(字段名): 该字段的最大值

                min(字段名): 该字段的最小值

               count(字段名):该字段的记录数

               sum(字段名): 该字段的总和

         聚合函数不能直接在where后面使用

      ---找出班级中年龄低于平均年龄的学员

       select * from student where age < (select avg(age) from student);

运算符:

      逻辑:    not(非)     or(或)      and(与)

        1.范围查询

               select * from 表名 where 字段名  [not]  between 值1 and 值2;

       2.在一组值中匹配

              select * from 表名 where 字段名 [not] in (值1,值2.......)

       3.将null作为索引的条件

              select * from 表名 where name is [not] null;

14.数据库的还原备份?

      数据库备份步骤:

      1)新开一个终端

      2) mysqldump -u root -p12345678 数据库名字1 > 新路径/数据库名字2.sql;

      3)回到之前的终端,删除旧的数据库: drop database 数据库名字1;

     4)新建一个数据库, 起名为: 数据库名字2;

     5)选择数据库:   use 数据库名字2;

     6)解析数据:  source 新路径/数据库名字2.sql;

15.Mysql优化?

      (1)  in 和 not in 要慎用,否则会导致全表扫描

           select  id from zhou where number  in (1,2,3);

    修改为: select id from zhou  where number between 1 and 3;

    (2) 应尽量避免在where子句中使用or来连接条件,

         否则会导致引擎放弃使用索引而进行全表扫描

       select id from  zhou where num = 10 or num = 20;

        修改为: select id from zhou where num = 10 

                       union all

                     select id from zhou where num = 20;

   (3) 如果在where子句中使用参数,也会导致全表扫描

         select id from zhou num = @num;

修改为强制查询使用索引:  select id from zhou with(index(索引名)) where num = @num;

   (4)尽量避免在where子句中对字段进行表达式操作,将导致引擎放弃使用索引而全表扫描

             select id from zhou where num/2 = 100;

       修改为: select id from zhou where num = 100 * 2;

  (5)任何时候都不要使用select * from zhou,用具体的字段代替 * ,不要返回不需要的字段

  (6)如果使用到临时表,在存储过程的最后务必将所有的临时显示删除.

            先 truncate table 然后 drop table 

           这样可以避免系统表的较长时间锁定

16.sql练习

---查询成绩表中name重复的

select name from score group by name having count(*) > 1;

---查询成绩表中name>2次,并且按照降序排列

select count(name) as '出现次数', name group by name having count(name)> 2 order by '出现次数' DESC;

 ---sql distinct 去掉重复的数据

      请在以下的表格,Store_Information,找出所有不同的店名,用 SQL 语句来查询

              

          select distinct store_name from Store_Information 

---在 Store_Information 表格中找出哪些店的营业额有超过 $1,500,用 SQL 语句来查询

          select store_name from Store_Information where Sales > 1500;

---如查询一个范围的薪资

        select store_name from Store_Information where salary > 1000 OR (salary < 500 AND salary > 275);

---模糊条件(like)查询:%C:以C结尾,  C%: 以C开头

                       select * from  表名 where 字段名 like '%条件%';

         Select *或者字段1,字段2,… from table_name where 字段1 like %A%;包含A的字符

         Select *或者字段1,字段2,… from table_name where 字段1 like A%;以A起头的字符

        Select *或者字段1,字段2,… from table_name where 字段1 like %A;以A结尾的字符

        Select *或者字段1,字段2,… from table_name where 字段1 like '_汉字_';一个下划线,表示一个字符,共计四个字符

        Select *或者字段1,字段2,… from table_name where 字段1 like '%汉字%'; %表示无限个字符

---范围查询(in/between...and...)

          -- 字段值可为数值、或字符类型

       Select * 或者字段1,字段2,… from table_name where 字段1 in (1,2,3,…)或者('质量','数量',…)…;

       Select * 或者字段1,字段2,… from table_name where 字段1 between 10 and 100;

---排序查询(order by…asc/desc 一般都是组合使用

         SELECT * FROM table_name  order by 字段1 asc;

         SELECT * FROM table_name  order by 字段2 desc;

         SELECT * FROM table_name  order by 字段1, 字段2,…  desc或asc;

         SELECT * FROM table_name where 字段1=数值1 order by 字段1 desc, 字段2 asc,…;

      http://blog.sina.com.cn/s/blog_49e3ed640102w0s2.html

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值