1 JDBC调取存储过程
-- 创建一个存储过程,只有in参数
create or replace procedure delete_test_student(sid_in in char) is exec delete_test_student('stu2'); |
-- 创建一个存储过程,有in参数,有out参数
create or replace procedure select_test_student(sid_in in test_student.sid%type,sname_out out test_student.sname%type) is set serveroutput on; |
-- 创建一个存储过程,out一个游标(即返回游标)
create or replace procedure retCursor(ret_cursor out sys_refcursor) is ret_cursor_value sys_refcursor; v_ename test_student.sname%type; begin open ret_cursor_value for select sname from test_student; ret_cursor:=ret_cursor_value; -- loop -- fetch ret_cursor into v_ename; -- exit when ret_cursor%notfound; -- dbms_output.put_line(v_ename); -- end loop; close ret_cursor_value; end; |
JDBC调取存储过程
public class OraclePro { //调用包含in参数和out参数的存储过程 } |
2 数据库连接池
数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。更为重要的是我们可以通过连接池的管理机制监视数据库的连接的数量﹑使用情况,为系统开发﹑测试及性能调整提供依据。
dbcp
import org.apache.commons.dbcp.BasicDataSource;
|
c3p0
在Java类中运用:
public class C3p0Test { //使用编码方式实现c3p0数据库连接池 @Test public void TestC3p0() throws PropertyVetoException, SQLException{ //第一步:创建连接池核心工具类 ComboPooledDataSource dataSource=new ComboPooledDataSource(); //第二步:连接池,url,驱动,账号,密码,初始连接数,最大连接数 dataSource.setJdbcUrl("jdbc:mysql:///test");//设置url dataSource.setDriverClass("com.mysql.jdbc.Driver");//设置驱动 dataSource.setUser("root");//mysql的账号 dataSource.setPassword("123456");//mysql的密码 dataSource.setInitialPoolSize(6);//初始连接数,即初始化6个连接 dataSource.setMaxPoolSize(50);//最大连接数,即最大的连接数是50 dataSource.setMaxIdleTime(60);//最大空闲时间 //第三步:从连接池对象中获取数据库连接 Connection con=dataSource.getConnection(); String sql="select * from user "; PreparedStatement ps=con.prepareStatement(sql); ResultSet rs=ps.executeQuery(); List<User> list=new ArrayList<User>(); while(rs.next()){ User user=new User(); user.setId(rs.getInt("id")); user.setName(rs.getString("name")); user.setPassword(rs.getString("password")); user.setEmail(rs.getString("email")); user.setPhone(rs.getString("phone")); list.add(user); } System.out.println(list); } } |
在配置文件中运用:
<c3p0-config> </c3p0-config> |
3 函数式编程
回顾:https://blog.csdn.net/qq_43711904/article/details/95589294