频道直达 - 专题 - 新闻 - 技巧 - 组网 - 开发 - 安全 - web编程 - 图像 - 操作系统 - 数据库 - 教育 - 旅游 - 健康 - 时尚 - 驱动 - 软件 - 游戏 - 多媒体 - ERP - 讨论组

Eclipse下配置swt开发环境

来源:本站原创 作者:佚名 出处:巧巧读书 2008-04-07 进入讨论组

    创建存储过程的脚本,

    使用sqlserver2000 中的pubs 数据库中的 jobs表为例.

   create procedure  showAll
    as
    select * from jobs

    create procedure obtainJob_desc
    @outputParam varchar(20) output,
    @id          int
    as
    select @outputParam = job_desc from jobs where job_id = @id


    create procedure obtainReturn
    as
    declare @ret int
    select @ret = count(*) from jobs
    return @ret


    declare @ret int
    exec  @ret = obtainReturn
    print @ret


  
    用来获得连接的函数

   public  Connection getConnection()...{
      Connection con = null;
      try ...{
       Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
       con = DriverManager.getConnection("jdbc:microsoft:sqlserver://localhost:1433;databasename=pubs","sa","");
      } catch (Exception e) ...{
       e.printStackTrace();
      }
      return con ;
     }

    1,调用得到结果集的存储过程

 public void getResultSet()...{
            //获得连接
            Connection con = this.getConnection();
            try ...{
                //showAll为存储过程名
                java.sql.CallableStatement cstm = con.prepareCall("{call showAll }");
                ResultSet rs = cstm.executeQuery();
                while(rs.next())...{
                    //这里写逻辑代码。
                    System.out.println(rs.getString(1));
                }
                rs.close();
                con.close();
            } catch (SQLException e) ...{
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

        2,调用带有 输入 ,输出 参数的存储过程。

  public void getOutParameter(int inParam)...{
            String outParam;
            Connection con = this.getConnection();

            try ...{
                CallableStatement cstm = con.prepareCall("{call obtainJob_desc (?,?)}");
                cstm.registerOutParameter(1, Types.VARCHAR);
                cstm.setInt(2, inParam);
                cstm.execute();;

                //得到输出参数。
                String outParma = cstm.getString(2);
                System.out.println(outParma);
                cstm.close();
                con.close();

            } catch (SQLException e) ...{
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    3,调用带返回值的存储过程。

 public void getReturn()...{
            int ret;
            Connection con = this.getConnection();
            try ...{
                CallableStatement cstm = con.prepareCall("{?=call obtainReturn()}");
                cstm.registerOutParameter(1, Types.INTEGER);

                cstm.execute();
                //得到返回值
                 ret = cstm.getInt(1);

                System.out.println(ret);
                cstm.close();
                con.close();

            } catch (SQLException e) ...{
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }

更多文章 更多内容请看电脑配置手册服务器配置专栏Eclipse应用技术专题,或进入讨论组讨论。
收藏此文】【 】【打印】【关闭
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
巧巧读书宗旨
相关专题
讨论组问题推荐
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
巧巧电脑频道编辑信箱  告诉我们您想看的专题或文章