熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

Eclipse下配置swt開發環境

2013-11-23 20:17:29  來源: Java開源技術 

  創建存儲過程的腳本

  使用sqlserver 中的pubs 數據庫中的 jobs表為例

      create procedure  showAll
    as
    select * from jobs

  create procedure obtainJob_desc
    @outputParam varchar() 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 {
       ClassforName(commicrosoftjdbcsqlserverSQLServerDriver);
       con = DriverManagergetConnection(jdbc:microsoft:sqlserver://localhost:;databasename=pubssa);
      } catch (Exception e) {
       eprintStackTrace();
      }
      return con ;
     }
    調用得到結果集的存儲過程

    public void getResultSet(){
            //獲得連接
            Connection con = thisgetConnection();
            try {
                //showAll為存儲過程名
                javasqlCallableStatement cstm = conprepareCall({call showAll });
                ResultSet rs = cstmexecuteQuery();
                while(rsnext()){
                    //這裡寫邏輯代碼
                    Systemoutprintln(rsgetString());
                }
                rsclose();
                conclose();
            } catch (SQLException e) {
                // TODO Autogenerated catch block
                eprintStackTrace();
            }

  }

  調用帶有 輸入 輸出 參數的存儲過程

     public void getOutParameter(int inParam){
            String outParam;
            Connection con = thisgetConnection();

  try {
                CallableStatement cstm = conprepareCall({call obtainJob_desc (??)});
                cstmregisterOutParameter( TypesVARCHAR);
                cstmsetInt( inParam);
                cstmexecute();;

  //得到輸出參數
                String outParma = cstmgetString();
                Systemoutprintln(outParma);
                cstmclose();
                conclose();

  } catch (SQLException e) {
                // TODO Autogenerated catch block
                eprintStackTrace();
            }

  }


    調用帶返回值的存儲過程

    public void getReturn(){
            int ret;
            Connection con = thisgetConnection();
            try {
                CallableStatement cstm = conprepareCall({?=call obtainReturn()});
                cstmregisterOutParameter( TypesINTEGER);

  cstmexecute();
                //得到返回值
                 ret = cstmgetInt();

  Systemoutprintln(ret);
                cstmclose();
                conclose();

  } catch (SQLException e) {
                // TODO Autogenerated catch block
                eprintStackTrace();
            }

  }

  


From:http://tw.wingwit.com/Article/program/Java/ky/201311/28246.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.