MySQL在JSP環境下的操作應用

來源:互聯網
上載者:User
js|mysql

前提:

       將MySQL資料庫的驅動放在工作目錄的web-inf\lib目錄下(這樣才能在JSP中連結上)

       用JavaBean串連,將編譯好得.class檔案放在classes檔案下,若檔案包含package指令,則要放到

指定的目錄下。

       此時,資料查詢沒問題,但是update,delete和insert都無效。(在SQL Server 中可行) 

       問題解決,察看JDK說明,找到Statement的方法段ResultSet executeQuery(String), int executeUpdate(String)

       修改JavaBean,添加executeUpdate方法,修改.jsp檔案,將非select時指向executeUpdate,測試update,insert,

delete都成功實現

       executeQuery方法代碼:

   public ResultSet executeQuery(String sqlString)
  {
   
   rs=null;
   try
   {
    
    conn=DriverManager.getConnection(connURL,userName,pwd);
    Statement stmt=conn.createStatement();
    rs=stmt.executeQuery(sqlString);
   }
   catch(SQLException ex)
   {
    System.err.println("aq.executeQuery:"+ex.getMessage());
   }
   
   return rs; 
  }

excuteUpdate方法代碼:

public int executeUpdate(String sqlString)
  { 
   instructionCount=0;
   try
   {
    
    conn=DriverManager.getConnection(connURL,userName,pwd);
    Statement stmt=conn.createStatement();
    stmt.executeUpdate(sqlString);
    instructionCount=1;
   }
   catch(SQLException ex)
   {
    System.err.println("aq.executeQuery:"+ex.getMessage());
   }
   
   return instructionCount; 
  }

  新問題:在MySQL使用utf-8來支援全中文時,再次對支付串進行編解碼會破壞中文的輸入,

在插入和更新資料時,取消原來用GBK的new String 來編碼

作者

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.