设为首页 加入收藏

TOP

MySQL 存储过程实例(二)
2015-11-21 03:26:25 来源: 作者: 【 】 浏览:24
Tags:MySQL 存储 过程 实例
re_data = 1,然后结束循环 END REPEAT;
最后关闭游标 close my_cursor;
因为上面在定义游标时,指明了,没有数据时设置了 no_more_data = 1,所以这里使用 UNTIL no_more_data = 1 来退出repeat
?
2)判断相等是使用 = ,而不是 == ,赋值操作是使用 set var=xxx; :set fuben_times = total_level / 2;
?
3. Java 如何调用存储过程
?
1)hibernate调用存储过程:
?
    /*
         * 调用无参数的存储过程,传入存储过程名字
         */
    public int callProcedure(final String procedureName)
    {
            int count = (Integer)this.getHibernateTemplate().execute(
                new HibernateCallback(){
                public Object doInHibernate(Session session) throws HibernateException, SQLException {
                    String procedureSql = "{call "+ procedureName +"()}";
                    Query query = session.createSQLQuery(procedureSql);
                    Integer num = query.executeUpdate();
                    return num;
                }
            });
            return count;
    }

?

2)ibatis 调用 mysql 存储过程:
?
    @Override
    public Long loginAndRegByProcedure(String user_Pwd, String user_MobileCode, String user_RegIP){
        Long userId = null;
        HashMap paramMap = new HashMap();  
        paramMap.put("userId", userId);  
        paramMap.put("user_Pwd", user_Pwd);  
        paramMap.put("user_MobileCode", user_MobileCode);  
        paramMap.put("user_RegIP", user_RegIP);  
        
        this.getSqlMapClientTemplate().queryForObject("Users.loginAndRegByProcedure", paramMap);  
        return (Long)paramMap.get("userId"); 
    }

?

对应的xml 文件配置:
?
  
      
      
      
      
  
  
      {call loginandreg(?, ?, ?, ?)}
  

?

?
存储过程的参数的类型,是在xml文件中说明的。
?
3) JDBC 调用mysql 存储过程:
?
    public Long loginAndRegByProcedure2(String user_Pwd, String user_MobileCode, String user_RegIP){
        Connection conn = DbUtil.getConnection();
        CallableStatement cstmt =  conn.prepareCall("{call loginandreg(?, ?, ?, ?)}");
        cstmt.setString(2, user_Pwd);
        cstmt.setString(3, user_MobileCode);
        cstmt.setString(4, user_RegIP);
        cstmt.registerOutParameter(1, java.sql.Types.BIGINT);
        cstmt.execute();
        return cstmt.getLong(1);
    }

?

输入参数:cstmt.setString(2, user_Pwd);
?
输出参数:cstmt.registerOutParameter(1, java.sql.Types.BIGINT);
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mysql会员数据导入导出 下一篇(1)mysql优化之sql性能问题定位

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: