通過表單為資料庫增添資料示例
阿新 • • 發佈:2019-01-23
<%@ page language="java" pageEncoding="utf-8" import="java.sql.*" %> <%request.setCharacterEncoding("gb2312"); %> <script language="JavaScript"> function check(theForm) { if(theForm.bh.value.length!=7) { alert("職工編號必須為7位!"); theForm.bh.focus(); return (false); } if(theForm.xm.value=="") { alert("請輸入姓名!"); theForm.xm.focus(); return (false); } if(theForm.csrq.value=="") { alert("請輸入出生日期!"); theForm.csrq.focus(); return (false); } if(theForm.jbgz.value=="") { alert("請輸入基本工資!"); theForm.jbgz.focus(); return (false); } if(theForm.gwjt.value=="") { alert("請輸入崗位津貼!"); theForm.gwjt.focus(); return (false); } return (true); } </script> <html> <head><title>職工增加</title></head> <body> <div align="center"> <p>職工增加</p> <form id="forml" name="forml" method="post" action="ZgZj0.jsp" onSubmit="return check(this)"> <table border="1"> <tr><td>編號</td><td><input name="bh" type="text" id="bh"/></td></tr> <tr><td>姓名</td><td><input name="xm" type="text" id="xm"/></td></tr> <tr><td>性別</td><td><input type="radio" name="xb" value="男" checked="checked"/>男 <input type="radio" name="xb" value="女"/>女 </td></tr> <% Class.forName("com.mysql.jdbc.Driver").newInstance();//匯入包時,複製到lib並右鍵包Build Path String url="jdbc:mysql://localhost:3306/rsgl"; String user="root"; String password="15876365651"; Connection conn=DriverManager.getConnection(url,user,password); String sql="select*from bmb order by bmbh"; Statement stmt=conn.createStatement(); ResultSet rs=stmt.executeQuery(sql); %> <tr><td>部門</td> <td><select name="bm"> <% while(rs.next()) { String bmbh=rs.getString("bmbh"); String bmmc=rs.getString("bmmc"); %> <option value="<%=bmbh%>"><%=bmmc%></option> <% } rs.close(); stmt.close(); conn.close(); %> </select> </td></tr> <tr><td>出生日期</td><td><input name="csrq" type="text" id="csrq"/></td></tr> <tr><td>基本工資</td><td><input name="jbgz" type="text" id="jbgz"/></td></tr> <tr><td>崗位津貼</td><td><input name="gwjt" type="text" id="gwjt"/></td></tr> </table> <br> <input name="submit" type="submit" value="確認"/> <input name="reset" type="reset" value="重置"/> </form> </div> </body> </html>
<%@ page language="java" pageEncoding="utf-8" import="java.sql.*" %> <%request.setCharacterEncoding("gb2312"); %> <html> <head><title></title></head> <body> <% String bh=request.getParameter("bh"); String xm=request.getParameter("xm"); String xb=request.getParameter("xb"); String bm=request.getParameter("bm"); String csrq=request.getParameter("csrq"); String jbgz=request.getParameter("jbgz"); String gwjt=request.getParameter("gwjt"); try{ Class.forName("com.mysql.jdbc.Driver").newInstance();//匯入包時,複製到lib並右鍵包Build Path String url="jdbc:mysql://localhost:3306/rsgl"; String user="root"; String password="15876365651"; Connection conn=DriverManager.getConnection(url,user,password); String sql="insert into zgb(bh,xm,xb,bm,csrq,jbgz,gwjt)"; sql=sql+"values('"+bh+"','"+xm+"','"+xb+"','"+bm+"','"+csrq+"'"; sql=sql+","+jbgz+","+gwjt+")"; Statement stmt=conn.createStatement(); int n=stmt.executeUpdate(sql); if(n>0){ out.print("職員記錄增加成功!"); } else{ out.print("職員記錄增加失敗!"); } stmt.close(); conn.close(); } catch(Exception e){ out.print(e.toString()); } %> </body> </html>