java面试题4

来源:互联网 发布:mysql 表连接 编辑:程序博客网 时间:2024/04/29 23:37

1、statement 和preparedstatement的区别 

    1.statementpreparedstatemnet的父类  

    2.statement是直接发送sql语句到数据库,事先没有进行预编译,这样会导致  

    恶意注入sql语句的问题出现.prepatedstatement会将sql进行预编译,当sql语句要重

    复执行时,数据库会调用以前编译好的sql,所以preparedstatement在性能方面会更好  

    3.preparedstatement在执行sql时,对传入的参数进行强制类型转换,以保证数据格式

    与底层数据库格式一致。  

    4.PreparedStatement相对于Statemnet要安全一些,可以有效防止sql注入.  

    5.PreparedStatement能够执行批处理,而Statement不行.  

     

    另:CallableStatement  是用来调用存储过程的    


PL/SQL部分:   

解释如下概念                                        

     1.触发器(trigger):是存储在数据库中的块,这些块一旦被构造后,就可以多次执行,

当触发它的事件发生时调用该触发器。触发事件是指对表中数据的操作,如插入(inserting)

删除(deleting)和修改(updating)。需要注意的是:触发器没有参数,没有返回值,不能够显

示调用。  

     create or replace trigger user_log      user_log该触发器的名字  

     before delete or update or insert  

     on t_user  

    for each row  

     begin  

     if deleting then  

     insert into t_user_log values  

     (myseq.nextval,'用户'||:old.username||'被删除了'); --:old 特殊的变量表示操作的原数据  

     elsif inserting then  

     insert into t_user_log values  

     (myseq.nextval,'用户'||:new.username||'被添加了');--new,特殊的变量表示操做的新数据  

     elsif updating then  

     insert into t_user_log values  

     (myseq.nextval,'用户'||:old.username||'被修改了'||:new.username);  

     else  

     null;  

     end if;  

     end;  

      

  2.存储过程(procedure):是将常用的或很复杂的工作,预先用SQL语句写好并用一个指定

的名称存储起来,  那么以后要叫数据库提供与已定义好的存储过程的功能相同的服务时,

需调用execute,即可自动运行。我的理解就是一堆sql  的集合,可以建立非常复杂的查询,

编译运行运行一次后,以后再运行速度比单独执行SQL快很多.  

  1、具有删除功能的存储过程  

  create or replace procedure  

  delete_stu(v_stuid number)  

  is  

  begin  

  delete from t_student where    

  sid=v_stuid;  

  end;  

  调用存储过程:  

  exec delete_stu(1001);  

   

3.游标(cursor):游标(cursor),是一种控制结构,可以帮助我们处理多条记录。游标不是一

种数据类型。  

    对于游标的使用:  

           1、声明游标  cursor cursor_name is select....  

           2、打开游标  open cursor_name  

           3、从游标中抓取数据  fetch cursor_name into v_row_data  

           4、关闭游标  close cursor_name  

静态游标的使用:  

     declare  

     v_data t_student%rowtype;  

     cursor mycursor    

     is    

     select * from t_student;  

     begin  

     open mycursor;  

     fetch mycursor into v_data;  

     dbms_output.put_line(v_data.stuname);  

     close mycursor;  

     end;  

 

动态游标的使用:  

  declare  

  type cur_type is ref cursor;  

  v_data t_student%rowtype;  

  v_mycursor cur_type;    

  begin  

  open v_mycursor    

  for    

  select * from t_student;  

  fetch v_mycursor into v_data;  

  while v_mycursor%found loop  

  dbms_output.put_line(v_data.stuname);  

  fetch v_mycursor into v_data;  

  end loop;  

  close v_mycursor;  

  end;  

             

4.索引(index):相当于书签,能够提高数据的访问效率  

     主键列自动创建索引  

     其他列必须手动创建索引。  

       

5.视图(view):虚拟的存在于数据库中。  

     用到视图的时候,系统会自动搜索数据库中的表,数据都是从表中取到的,  

     并不是从视图中直接拿到的。  

                                         

6.函数(function):必须有返回值,一般用于查询。  

  create or replace function emp_income  

  (v_empno emp_lc.empno%type)  

  return number  

  is  

  v_income number(10,2);  

  begin  

  select sal*12+nvl(comm,0)*12  

  into v_income  

  from emp_lc where empno=v_empno;  

  return v_income;  

  end;  

     

7.序列(sequence):oracle数据库没有提供主键自动增长,为了方便操作,  

oracle数据库提供了序列,它的作用就是能够间接的实现主键自增长。

0 0
原创粉丝点击