jsp中調用Bean,然後在Bean中調用oracle預存程序。

來源:互聯網
上載者:User
js|oracle|預存程序 新手在寫程式時,一定要膽大心細,而且要有耐心,不妥協,不懂就翻書,網上查資料,問朋友,堅決進行到底。

     最近一直憑著ASP的知識在摸索中前進,一跑坎坷,自不用說了。 言歸正傳。

     建立一個登入系統,要求達到以下目的。

     1、使用者通過Bean來認證以及得到得到使用者資訊。

     2、記錄使用者登入資訊,如使用者登入次數,最後登入時間。

     3、記錄動作記錄。

    未解決及疑惑的問題:

    1、使用者登入後的Session是否可以通過Bean來判斷。

    2、通過Bean調用ORACLE預存程序,返回select後的記錄集。

   操作步驟:

   1、建立使用者驗證Bean:

    public boolean checkUser() throws Exception {
    boolean flag=false;
    ResultSet rs=conn.executeQuery(getSql());
    if(rs.next()){
      userID        =rs.getString("userID");
      userName      =rs.getString("userName");
      userPWD       =rs.getString("userPWD");
      userUnit      =rs.getString("userUnit");
      userLoadTime  =rs.getDate("userLoadTime");
      userLoadNumeric=rs.getInt("userLoadNumber");
      flag=true;
    }
    rs.close();
    conn.closeConn();
    return flag;
  }
  

  通過返回的值判定使用者是否存在。

 2、記錄使用者登入資訊:

  public void changeLoginInfo(String userID) throws Exception{
    String sql="update SystemUserTable set UserLoadTime=sysdate,UserLoadNumber=UserLoadNumber+1 where userID='"+userID+"'";
    conn.executeUpdate(sql);
  }

3、記錄動作記錄:

第一步,建立預存程序

create or replace procedure proc_writeNote(
  description in varchar2,
  wName in varchar2,
  wIP in varchar2
  )
is
begin
  insert into Systemnote (Id,Description,Wname,Wip) values(Autoaddid.Nextval,description,wName,wIP);
  commit;
end proc_writeNote;

第二步、建立操作預存程序的方法(重寫prepareCall()方法)

  public CallableStatement prepareCall(String produce){
    try {
      conn = DriverManager.getConnection(DBUrl, UserID, UserPWD);
      cstmt=conn.prepareCall(produce);
    }
    catch (SQLException ex) {
      System.err.print("prepareCall():"+ex.getMessage());
    }
    return cstmt;
  }

第三步,執行預存程序

  public void writeNote(String description,String wName,String wIP){
    String sql="{call proc_writeNote(?,?,?)}";
    try {
      CallableStatement cstmt=conn.prepareCall(sql);
      cstmt.setString(1, description);
      cstmt.setString(2,wName);
      cstmt.setString(3,wIP);
      cstmt.executeUpdate();
    }
    catch (SQLException ex) {
      System.out.print("writeNote():"+ex.getMessage());
    }
  }



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.