JDBC调取存储过程&数据库连接池&函数式编程

1 JDBC调取存储过程

-- 创建一个存储过程,只有in参数

create or replace procedure delete_test_student(sid_in in char) is
begin
    delete from test_student where sid=sid_in;
end;
/

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
begin
    select sname into sname_out from test_student where sid=sid_in;
end;
/

set serveroutput on;
declare
    sname_out test_student.sname%type;
begin
    select_test_student('stu1',sname_out);
    dbms_output.put_line(sname_out);
end;
/

-- 创建一个存储过程,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参数的存储过程
    public void callInOutPro() throws Exception {
        Connection conn = OracleUtils.getConnection();
        //参数一:in char型;参数二:out char型
        String sql = "{call select_test_student(?,?)}";
        CallableStatement cs = conn.prepareCall(sql);
        cs.setObject(1,"stu1"); //in
        cs.registerOutParameter(2, Types.CHAR); //out
        //如果有返回值
        //cs.registerOutParameter(3,Types.CHAR)
//        //out游标时
//        cs.registerOutParameter(2,OracleTypes.CURSOR);
//        //游标结果的获取
//        ResultSet rs = (ResultSet)cs.getObject(2);
//        ResultSetMetaData rd = rs.getMetaData();
//        int count = rd.getColumnCount();
//        while(rs.next()) {
//            for(int i = 1; i <= count; i++){
//                //输出
//            }
//        }
        cs.execute(); //执行调用存储函数
        char sname = (char) cs.getObject(2);
        OracleUtils.closeConnection(conn);
    }

}

2 数据库连接池

    数据库连接池的基本思想就是为数据库连接建立一个“缓冲池”。预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需从“缓冲池”中取出一个,使用完毕之后再放回去。我们可以通过设定连接池最大连接数来防止系统无尽的与数据库连接。更为重要的是我们可以通过连接池的管理机制监视数据库的连接的数量﹑使用情况,为系统开发﹑测试及性能调整提供依据。
    dbcp

import org.apache.commons.dbcp.BasicDataSource;


public class JDBCUtils {
    private static String driver = "com.mysql.cj.jdbc.Driver";
    private static String url="jdbc:mysql://localhost:3306/mydatabase?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=GMT%2B8";
    private static String username = "root";
    private static String password = "123456";
    
    private static BasicDataSource basicDataSource = new BasicDataSource();
    static{
        basicDataSource.setUrl(url);
        basicDataSource.setDriverClassName(driver);
        basicDataSource.setUsername(username);
        basicDataSource.setPassword(password);
        
        basicDataSource.setInitialSize(10);//初始化时创建10个链接
        basicDataSource.setMaxActive(8);//设置最大连接数
        basicDataSource.setMaxIdle(5);//这只最大的空闲连接数
        basicDataSource.setMinIdle(1);//设置最小空闲连接数字
    }
    //返回一个数据源DataSource
    public static DataSource getDataSource(){
        return 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默认配置,下面还可以配置多个数据库 -->
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test
        </property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <property name="initialPoolSize">6</property>
        <property name="maxPoolSize">50</property>
        <property name="maxIdleTime">1000</property>
    </default-config>

</c3p0-config>

3 函数式编程

回顾:https://blog.csdn.net/qq_43711904/article/details/95589294

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值