MySQL学习笔记:储存过程,MySQL和Java等的JDBC连接,防注入,配置文件【诗书画唱】
存储过程:
存储了多条sql语句的过程
存储过程的特点:
1.可以完成比较复杂的运算
2.SQL编程的代码可重复使用
3.执行的速度相对快一些(每次写的sql语句数据库会将其编译成函数,
按照存储过程的参数分类:
in:可以将参数带入到存储过程中去使用。
out:可以将参数当做返回值来进行返回内容。
存储过程数据库只会编译一次之后下次用的时候就不会再编译了)
语法:
create procedure 存储过程名字(先写参数,后写类型)
begin
做存储过程的sql语句
if(条件表达式)then
内容1
else
内容2
end if
end;
存储过程While语法:
while(条件表达式) do
循环的内容
end while;
存储过程Switch语法:
case num
when 3 then
sql语句
when 4 then
sql语句
else
其他sql语句
end case;
执行存储过程语句的对象
CallableStatement cs=con.prepareCall("{call select_stu()}");
// 1.导包
// 2.加载驱动(类加载器)使用某个文件
Class.forName("com.mysql.jdbc.Driver");
// 3.获取数据库链接(创建连接数据库的对象)
String url="jdbc:mysql://localhost:3306/chufaqi";
Connection con=DriverManager.getConnection(url,"root","root");
// 4.执行sql语句的对象
PreparedStatement ps=con.prepareStatement("select * from stu1");
// 5.声明一个接收结果集的对象
ResultSet res=ps.executeQuery();
// 6.遍历结果集对象
while(res.next()){
System.out.println(res.getObject(1)+"\t"+res.getObject(2)+"\t"+res.getObject(3)+"\t"+res.getObject(4));
}