通过表单为数据库增添数据示例


<%@ 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>


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值