JSP(4) - 增加客戶模組

zhyuh發表於2005-06-01

分成before和after兩個介面。before介面用於讓使用者輸入顧客資訊,after介面用於後臺處理。

要點

1)jsp中引入java類



2)得到系統當前時間,並自定義格式顯示在網頁中

DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE); %>

......

    最後修改日起:

    
3)在網頁之間傳遞資料

before頁: 

 

after頁接收資料:

變數sCustID就得到了上頁文字框cust_id中的值

4)資料提交後將submit鍵灰掉

 

[@more@]

add_cust_bef.jsp





response.setHeader("Cache-Control","no-store"); //HTTP 1.1
response.setHeader("Pragma","no-cache"); //HTTP 1.0
response.setDateHeader("Expires", 0); //prevents caching at the proxy server

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
Date d = new Date();
DateFormat dFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINESE);
%>


 


   
   
    增加客戶資訊
   
   
   
   
   
   
       
    <!--
   
    --&gt
 
 
 

   

增加客戶


   

客戶卡號:     

    客戶姓名:     

    性別:        
                                                        

    出生年月:     
   

    通訊地址:     

    郵政編碼:     
  
    辦公室電話:   
 
    家庭電話:     
  
    手機:         
 
    Email:        

    折扣:         %
   
    累計購物金額:

    增加日期:     
       
    最後修改日起:

     
   

           




  

 
 

add_cust_aft.jsp







增加客戶資訊


String sCustID = request.getParameter("cust_id");
String sCustName = request.getParameter("cust_name");
String sCustSex = request.getParameter("cust_sex");
String sCustYear = request.getParameter("cust_year");
String sCustMonth = request.getParameter("cust_month");
String sCustAddr = request.getParameter("cust_addr");
String sCustZip = request.getParameter("cust_zip");
String sCustPhOff = request.getParameter("cust_ph_office");
String sCustPhHom = request.getParameter("cust_ph_home");
String sCustMob = request.getParameter("cust_mobile");
String sCustEmail = request.getParameter("cust_email");
Float  fCustDis = Float.valueOf(request.getParameter("cust_discount"));
Float  fCustTtSum = Float.valueOf(request.getParameter("cust_trd_sum"));
String dCustAdd = request.getParameter("cust_crt_date");   
String dCustUpt = request.getParameter("cust_upt_date");

String sCustID2 = new String(sCustID.getBytes("iso-8859-1"),"GBK");
String sCustName2 = new String(sCustName.getBytes("iso-8859-1"),"GBK");
String sCustAddr2 = new String(sCustAddr.getBytes("iso-8859-1"),"GBK");
String sCustPhOff2 = new String(sCustPhOff.getBytes("iso-8859-1"),"GBK");
String sCustPhHom2 = new String(sCustPhHom.getBytes("iso-8859-1"),"GBK");

if (sCustID.equals("")){
 throw new Exception("客戶卡號不準為空.");
}
if (sCustName.equals("")){
 throw new Exception("客戶姓名不準為空");
}

Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
String url="jdbc:oracle:thin:@TCSCH074.tcsgdccn.com:1521:orcl";
//orcl為你的資料庫的SID
String user="store";
String password="store";
Connection conn= DriverManager.getConnection(url,user,password);
String sInsert = null;
Statement stmt = null;

try{

sInsert = "insert into customer(cst_id,cst_name,cst_sex, cst_year,cst_month,cst_address,cst_zip_code,";
sInsert = sInsert + "cst_ph_office, cst_ph_home, cst_mobile, cst_email, cst_discount, cst_trd_sum,";
sInsert = sInsert + "cst_crt_date, cst_upt_date) values('"+sCustID2+"','"+sCustName2+"','"+sCustSex+"','"+sCustYear+"','"+sCustMonth
  +"','"+sCustAddr2+"','"+sCustZip+"','"+sCustPhOff2+"','"+sCustPhHom2+"','"+sCustMob+"','"+sCustEmail+"',"+
  fCustDis+","+fCustTtSum+",to_date('"+dCustAdd+"','yyyy-mm-dd'),to_date('"+dCustUpt+"','yyyy-mm-dd'))";

stmt = conn.createStatement();
stmt.execute(sInsert);
conn.commit();
out.println("增加使用者"+sCustName2+"完成");
}
catch(Exception e){
 System.out.println("新增使用者失敗!");
 e.printStackTrace();
 out.println("

新增使用者失敗!

");
 out.println( e.getMessage() );
 out.println("

");
}
finally{
  if(stmt != null) stmt.close();
  conn.close();
}

%>



來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/207/viewspace-799591/,如需轉載,請註明出處,否則將追究法律責任。

相關文章