Site hosted by Angelfire.com: Build your free website today!

My Home Page

My Favorite things about Angelfire.

My Favorite Web Sites

Angelfire - Free Home Pages
Free Web Building Help
Angelfire HTML Library
HTML Gear - free polls, guestbooks, and more!

<%@ page contentType="text/html; charset=Big5" import="java.sql.*, java.util.Date"%> 將表單中的資料新增至資料庫

將表單中的資料新增至資料庫

<% String strSub = request.getParameter("submit"; //取得指令按鈕傳出的資料 //判斷是否取得指令按鈕傳出的資料 //若未取得則建立表單供使用者輸入資料 //若有取得資料, 則將資料新增至資料庫中 if(strSub == null) { session.setAttribute("Done",""; //重設變數值 Date Now = new Date() ; String date = (Now.getYear() + 1900) + "/"; date = date + (Now.getMonth() + 1) + "/"; date = date + Now.getDate(); %>
Olympus Promoter Weeekly Sales Record
PROMOTER ID:
PROMOTER NAME:
WORKING PERIOD:
DATE: >
SUPERVISOR NAME:
<% } else { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"; //載入驅動程式類別 Connection con = DriverManager.getConnection("jdbcdbcowerFunSystem"," "," "; //建立資料庫連線 Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); //建立Statement物件, 並設定記錄指標類型為可前後移動 String strSQL; if (session.getAttribute("Done" != "yes" {//判斷Done變數的值, 避免使用者按下 重新整理 按鈕重覆新增資料 strSQL = "INSERT INTO SalesRecord(Promoter_id, Promoter_name, Working_hour, " + "Record_date, Supervisor_name) Values ('" + request.getParameter("Promoter_id" + "','" + request.getParameter("Promoter_name" + "','" + request.getParameter("Working_hour" + "','" + request.getParameter("Record_date" + "','" + request.getParameter("Supervisor_name" + "')"; //建立新增資料至資料庫的SQL敘述 stmt.executeUpdate(strSQL); //利用SQL敘述將資料新增至資料庫 } session.setAttribute("Done", "yes"; //將session物件中的Done設為yes時, //避免使用者按下 重新整理 按鈕, 導致資料的重覆新增 %>

已將資料新增至BookIns資料表, 該資料表的內容如下:

<% ResultSet rs = stmt.executeQuery("SELECT * FROM SalesRecord"; //執行SQL敘述, 從資料庫中取得BookIns資料表中的資料 %> <% //利用while迴圈配合next方法將資料表中的記錄列出 while(rs.next()) { %> <% } %>
Promoter IdPromoter Name Working HourRecord Data Supervisor Name
<%= rs.getString("Promoter_id" %> <%= rs.getString("Promoter_name" %> <%= rs.getString("Working_hour" %> <%= rs.getString("Record_data" %> <%= rs.getString("Supervisor_name" %>
<% //關閉各物件 if(rs != null) rs.close(); if(stmt != null) stmt.close(); if(con != null) con.close(); %>

還要新增資料

<% } %>