java调用存储过程

 

/**
 * @Auther: zml
 * @Date: 2018/6/8
 */

import org.springframework.jdbc.core.CallableStatementCallback;
import org.springframework.jdbc.core.CallableStatementCreator;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

/**
 * @Auther: zml
 * @Date: 2018/6/8
 */
public class Test {

    private JdbcTemplate jdbcTemplate;

    public JdbcTemplate getJdbcTemplate() {
        return jdbcTemplate;
    }

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }


    //调用存储过程,一个输入参数,三个输出
    public List<String> callPro(String str){
        //final String str = str.toString();
        List<String> resList = (List)jdbcTemplate.execute(
            new CallableStatementCreator(){
                @Override
                public CallableStatement createCallableStatement(Connection connection) throws SQLException {

                    String storedPro = "{call KPI.CHECK_SITUATION(?,?,?,?)}";
                    CallableStatement cs = connection.prepareCall(storedPro);
                    cs.setString(1, str);
                    //输出参数类型
                    cs.registerOutParameter(2, Types.INTEGER);
                    cs.registerOutParameter(3, Types.VARCHAR);
                    cs.registerOutParameter(4, Types.VARCHAR);
                    return cs;
                }

            },(cs)->{
                    cs.execute();
                    List<String> list = new ArrayList<String>();
                    list.add(cs.getInt(2)+"");
                    list.add(cs.getString(3));
                    list.add(cs.getString(4));
                    return list ;

            }
        );
        return resList;
    }



    public static void xxmain(String[] args){
        Test t = new Test();
        //调用存储过程,传一个空的字符串
        t.callPro("");

    }
}

 

posted @ 2018-08-08 17:01  zouminglan  阅读(364)  评论(0编辑  收藏  举报