<%@ page contentType="text/html;charset=GB2312"%>
<%@ page import="java.sql.*"%>
<%!
   public String getstr(String str){
     try{
        String temp_p=str;
        byte[] temp_t=temp_p.getBytes("ISO8859-1");
        String temp=new String(temp_t);
        return temp;
     }
     catch(Exception e){
     }
     return null;
  }
%>
<html>
  <title>提交表单示例</title>
  <body>
     <p align="center"><b>提交表单示例</b></p>
      <form action="insert.jsp" method=post>
         <table>
            <caption>Enter User Info.</caption>
            <tr>
              <td>UserID</td>
              <td><input type=text size=50 name=dm></td>
            </tr>
            <tr>
              <td>name</td>
              <td><input type="text" size=50 name=xm></td>
            </tr>
            <tr>
              <td>password</td>
              <td><input type="text" size=50 name=mm></td>
            </tr>
            <tr> 
              <td>e_mail</td>
              <td><input type="text" size=50 name=email></td>
            </tr>      
         </table>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <input type="submit" value="submit" name=submit></form>
      <%
      }else{//002
        //使用Oracle数据库，驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("JDBC:oracle:thin:@IP:端口:实例","user","pwd");
        //建立Statement对象
        Statement stmt=con.createStatement(ResultSet.type_scroll_insensitive,ResultSet.concur_updatable); 
        if(session.getAttribute("Done")!="yes")
        {//003
           String temdm,temxm,temmm,tememail;
           temdm=getstr(request.getParameter("dm")); 
           temxm=getstr(request.getParameter("xm"));
           temmm=getstr(request.getParameter("mm"));
           tememail=getstr(request.getParameter("email"));
           //输入有效性检验
           if(temdm.equals("")||temxm.equals("")||temmm.equals("")||tememail.equals("")){
               response.sendRedirect("insert.jsp");
           } 
          //生成SQL语句
          String sqlstr="insert into userd values(?,?,?,?)";
          PreparedStatement pstmt=con.prepareStatement(sqlstr);  
          pstmt.setString(1,temdm);
          pstmt.setString(2,temxm);
          pstmt.setString(3,temmm);
          pstmt.setString(4,tememail);
          //利用SQL语句添加数据
          pstmt.executeUpdate();
        
        }
        //这里将防止表单重复提交
        //将session对象中的Done设置为yes
        //避免使用者单击刷新按钮重复动作
        session.setAttribute("Done","yes");
       
       %>
        <p>用户信息:</p>
       <%
        //建立ResultSet对象，并执行SQL语句
        ResultSet rs=stmt.executeQuery("select * from user"); 
       %>
       <table border>
          <tr>
             <td width="32">UserID</td>
             <td>name</td>
             <td>password</td>
             <td>e_mail</td>
          </tr>
          <%
           //利用while循环输出各条记录
           while(rs.next()){
           %>
             <tr>
               <td width="32"><%=rs.getString("dm")%></td>
               <td><%=rs.getString("xm")%></td>
               <td><%=rs.getString("mm")%></td>
               <td><%=rs.getString("email")%></td>
             </tr>
           <%
           }
          %>
       </table>
       <%
         rs.close();
         stmt.close();
         con.close();
       %> 
       <hr>
       <a href="insert.jsp">new</a>
      <%
      }//end002
     %>
  </body>
</html>
