我编写了以下代码,通过表单将数据插入到数据库中,但是在加载页面时会自动将空值插入到数据库中,请帮助我。 如果可能的话,请使用spring mvc,mysql和jsp为我提供一个简单的例子。
<%@include file="header.jsp" %>
<!-- topbar ends -->
<div class="ch-container">
<div class="row">
<!-- left menu starts -->
<div class="col-sm-2 col-lg-2">
<div class="sidebar-nav">
<div class="nav-canvas">
<div class="nav-sm nav nav-stacked">
</div>
<ul class="nav nav-pills nav-stacked main-menu">
<li class="nav-header">Main</li>
<li><a class="ajax-link" href="deshboard.jsp"><i class="glyphicon glyphicon-home"></i><span> Home</span></a></li>
<li><a class="ajax-link" href="view_emp_list.jsp"><i class="glyphicon glyphicon-eye-open"></i><span> Employee List</span></a></li>
</ul>
<label id="for-is-ajax" for="is-ajax"><input id="is-ajax" type="checkbox"> Ajax on menu</label>
</div>
</div>
</div>
<!--/span-->
<!-- left menu ends -->
<noscript>
<div class="alert alert-block col-md-12">
<h4 class="alert-heading">Warning!</h4>
<p>You need to have <a href="http://en.wikipedia.org/wiki/JavaScript" target="_blank">JavaScript</a>
enabled to use this site.</p>
</div>
</noscript>
<div id="content" class="col-lg-10 col-sm-10">
<!-- content starts -->
<div>
<ul class="breadcrumb">
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">Blank</a>
</li>
</ul>
</div>
<div class="row">
<div class="box col-md-12">
<div class="box-inner">
<div class="box-header well" data-original-title="">
<h2><i class="glyphicon glyphicon-edit"></i> Recruitment</h2>
<div class="box-icon">
<a href="#" class="btn btn-setting btn-round btn-default"><i
class="glyphicon glyphicon-cog"></i></a>
<a href="#" class="btn btn-minimize btn-round btn-default"><i
class="glyphicon glyphicon-chevron-up"></i></a>
<a href="#" class="btn btn-close btn-round btn-default"><i
class="glyphicon glyphicon-remove"></i> </a>
</div>
</div>
<div class="box-content">
<form method="POST" action="<%
String emp_idno=request.getParameter("emp_idno");
session.setAttribute( "theEmp_Id_No", emp_idno );
String emp_cid=request.getParameter("emp_cid");
String emp_fname=request.getParameter("emp_fname");
String emp_mname=request.getParameter("emp_mname");
String emp_lname=request.getParameter("emp_lname");
String sql="insert into tblemployee(fldEmployment_No,fldCitizenship_Id_No,fldFirst_Name,fldMiddle_Name,fldLast_Name) "
+ "values('"+emp_idno+"','"+emp_cid+"','"+emp_fname+"','"+emp_mname+"','"+emp_lname+"')";
try{
int action=conn.createStatement().executeUpdate(sql);
conn.setAutoCommit(true);
if (action >= 1){
String site = new String("view_emp.htm");
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader("Location", site);
}else{
// out.println("Not saved");
}
}catch(Exception e){
e.printStackTrace();
}
%>" >
<div class="form-group col-md-6">
<label class="control-label" for="inputSuccess4">Employment Id</label>
<input name="emp_idno" type="text" class="form-control" id="inputSuccess4">
</div>
<div class="form-group col-md-6">
<label class="control-label" for="inputSuccess4">Citizenship ID Card No or Passport No</label>
<input name="emp_cid" type="text" class="form-control" id="inputSuccess4">
</div>
<div class="form-group col-md-4">
<label class="control-label" for="inputSuccess1">* First Name</label>
<input name="emp_fname" type="text" class="form-control" id="inputSuccess1">
</div>
<div class="form-group col-md-4">
<label class="control-label" for="inputWarning1">Middle Name</label>
<input name="emp_mname" type="text" class="form-control" id="inputWarning1">
</div>
<div class="form-group col-md-4">
<label class="control-label" for="inputError1">* Last Name</label>
<input name="emp_lname" type="text" class="form-control" id="inputError1">
</div>
<div class="form-group col-md-12">
<label for="control-label">Photograph</label>
<input type="file" id="exampleInputFile">
<p class="help-block">Accepts .jpg, .png, .gif up to 1MB. Recommended dimensions: 200px X 200px</p>
</div>
<div class="form-group">
<button type="submit" class="btn btn-default">Submit</button>
<button type="reset" class="btn btn-default">Cancel</button>
</div>
</form>
</div>
</div>
</div>
<!--/span-->
</div><!--/row-->
<!-- content ends -->
</div><!--/#content.col-md-0-->
</div><!--/fluid-row-->
<hr>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Settings</h3>
</div>
<div class="modal-body">
<p>Here settings can be configured...</p>
</div>
<div class="modal-footer">
<a href="#" class="btn btn-default" data-dismiss="modal">Close</a>
<a href="#" class="btn btn-primary" data-dismiss="modal">Save changes</a>
</div>
</div>
</div>
</div>
<%@include file="footer.jsp" %>
答案 0 :(得分:0)
它将进行插入,因为在加载此jsp页面时会发生数据插入。
创建一个单独的jsp来保存表单输入组件,并使其表单操作为您为数据插入提供逻辑的jsp,或者使用servlet进行数据插入。
还要考虑在请求参数上添加非空验证