1 / 12

系統開發細部流程 教師:游峰碩

崑山科技大學資訊管理系 伺服網頁程式設計. 系統開發細部流程 教師:游峰碩. 流程步驟. 最好能夠先畫好一個案例的流程。 資料庫表格已經先建構好。 1. 先設計整個流程中所需之網頁 2. 佈署 web.xml 檔案 3. 建立對應的 Servlet 3.1 封裝從客戶端傳過來的資料成為 Java Bean 3.2 撰寫 Servlet 中的處理程式碼. 圖示. 1. 取出客戶端輸入之資料. 設計整個流程 中所需之網頁. 2. 封裝資料為 Java Bean. 佈署 web.xml 檔案. 3. 將資料寫入到資料庫. 建立對應的

tara
Download Presentation

系統開發細部流程 教師:游峰碩

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 崑山科技大學資訊管理系 伺服網頁程式設計 系統開發細部流程教師:游峰碩

  2. 流程步驟 • 最好能夠先畫好一個案例的流程。 • 資料庫表格已經先建構好。 • 1. 先設計整個流程中所需之網頁 • 2. 佈署web.xml檔案 • 3. 建立對應的Servlet • 3.1 封裝從客戶端傳過來的資料成為 Java Bean • 3.2 撰寫Servlet 中的處理程式碼

  3. 圖示 1 取出客戶端輸入之資料 設計整個流程 中所需之網頁 2 封裝資料為 Java Bean 佈署 web.xml檔案 3 將資料寫入到資料庫 建立對應的 Servlet 4 根據結果,顯示 成功或失敗訊息

  4. 建立對應的 Servlet Servlet 樣版程式碼 import java.io.IOException; import java.io.PrintWriter; import javax.servlet.*; import javax.servlet.http.*; publicclass XXXServlet extends HttpServlet { publicvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } publicvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { process(request, response); } publicvoid process(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html; charset=big5"); PrintWriter out = response.getWriter(); // 1. 取出客戶端輸入之資料 // 2. 封裝資料為 Java Bean // 3. 將資料寫入到資料庫,這裡,我們將它獨立出來成為一個方法。 // 4. 根據結果,顯示成功或失敗訊息 } } 可以使用它作為servlet的樣版,需要修改類別名稱。

  5. Servlet中處理的四大流程 1 取出客戶端輸入之資料 2 封裝資料為 Java Bean 3 將資料寫入到資料庫 4 根據結果,顯示 成功或失敗訊息

  6. 1 取出客戶端輸入之資料 1.取出客戶端輸入之資料 request.getParameter(“form 的元件名稱"); 有亂碼時,可以用來轉碼 String a = new String(b.getBytes("ISO8859_1"), "big5");

  7. 2 封裝資料為 Java Bean 2.封裝資料為 Java Bean 已經設計好Java Bean的話,就直接可以用 new 來建立。 任何尚未建構的Java Bean,可依據案例的需要自行設計。

  8. 3 將資料寫入到資料庫 將資料寫入到資料庫 • 資料庫動作有 • C (Create) 新增 • R (Retrieve) 存取,查詢 • U (Update) 更新 • D (Delete) 刪除

  9. C (Create) 新增樣版 public boolean writeToDB(我的JavaBean xxx){ boolean result = false; String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\eclipse\\workspace\\T093000300\\mydb.mdb"; try{ // 1. 取得資料庫聯結 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection(url, "",""); // 2. 建立好SQL字串 String insertSQL = "Insert into … … “; System.out.println(insertSQL); // 印出動態產生的SQL,是用來除錯用的。 // 3. 執行INSERT. 要用 executeUpdate Statement stmt = conn.createStatement(); stmt.executeUpdate(insertSQL); // 4. 一定要記得關閉 conn.close(); // 5. debug System.out.println("資料新增成功"); result = true; }catch(Exception e){ e.printStackTrace(); } return result; }

  10. R (Retrieve) 存取,查詢樣版 public ArrayList getMemberByName(String name){ ArrayList result = new ArrayList(); // 先宣告要回傳的變數 String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\eclipse\\workspace\\T093000300\\mydb.mdb"; try{ // 1. 取得資料庫聯結 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection(url, "",""); // 2. 建立好SQL字串 String sql = "SELECT * from … …"; System.out.println(sql); // 用來除錯 // 3. 執行SELECT. 要用 executeQuery Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); // 4. 得到結果,處理。 // 重點:這裡才是需要封裝資料成為JavaBean的地方。 // 因為資料可能很多筆,但不管何種情況,通通用 ArrayList來回傳 while(rs.next()){ Member m = new Member(); m.setName(rs.getString("name")); m.setAge(rs.getInt("age")); m.setAddress(rs.getString("address")); m.setTelephone(rs.getString("telephone")); m.setGender(rs.getInt("gender")); result.add(m); } // 5. 一定要記得關閉 conn.close(); }catch(Exception e){ e.printStackTrace(); } return result; }

  11. U (Update) 更新樣版 public boolean updateToDB(Member m){ boolean result = false; String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=C:\\eclipse\\workspace\\T093000300\\mydb.mdb"; try{ // 1. 取得資料庫聯結 Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); Connection conn = DriverManager.getConnection(url, "",""); // 2. 建立好SQL字串 String insertSQL = "update member … …“; System.out.println(insertSQL); // 用來除錯 // 3. 執行INSERT. 要用 executeUpdate Statement stmt = conn.createStatement(); stmt.executeUpdate(insertSQL); // 4. 一定要記得關閉 conn.close(); // 5. debug System.out.println("資料更新成功"); result = true; }catch(Exception e){ e.printStackTrace(); } return result; }

  12. D (Delete) 刪除樣版

More Related