【Javaweb】实现将前端JSP表单信息存入后端数据库

主要思路就是前端使用form表单,通过按钮提交之后在servlet层调用数据库实现方法即可。
前端JSP页面form表单:

<form action="Inputservlet" method="post">
    	<input type="hidden" name="oper" value="save_data"/>
	    <ul class="forminfo">
	    <li><label>入库单号</label><input name="add_bill_in_id" type="text" class="dfinput"><i>请符合单据规范填写</i></li>
	    
	    <li> 商品ID       
	    <select name="add_good_id" style="color:#FF6600"  >	
	    <option value='0'>请选择商品ID</option>    
	    <%
        List<String> list_good_id = (List<String>)request.getAttribute("list_good_id");	
	    for(String id : list_good_id){        	   		
        %> 
	    <option><%=id %></option>
	    <%} %>
	    </select>	    
	    </li>
	    
	    <li><label>入库数量</label><input name="add_count" type="text" class="dfinput"></li>
	    
	    <li> 负责人ID &nbsp&nbsp&nbsp&nbsp&nbsp      
	    <select name="add_user_id" style="color:#FF6600"  >	 
	    <option value='0'>请选择负责人ID</option>   
	    <%
        List<String> list_user_id = (List<String>)request.getAttribute("list_user_id");	
	    for(String id : list_user_id){        	   		
        %> 
	    <option><%=id %></option>
	    <%} %>
	    </select>	    
	    </li>	    
	    
	    <li><label>日期</label><input name="add_date" type="text" class="dfinput"></li>
	    <li><label>&nbsp;
	    </label>
	    <a href="javascript:void(0)" id="save_data">
	    <button type="submit" style='background-color:#FF6600;width:350px;height:40px;'>
	         录入
	    </button>
	    </a>
	    </ul>
	    <li>提示:请您按照规定格式录入,否则录入失败!</li>
    </form>

servlet调用AddInputInfo方法:

private void save_data(HttpServletResponse response, HttpServletRequest request) throws IOException {
		// TODO Auto-generated method stub
		System.out.println("执行入库信息保存功能");
		String bill_in_id = request.getParameter("add_bill_in_id");
		String good_id = request.getParameter("add_good_id");
		String countstring = request.getParameter("add_count");
		String user_id = request.getParameter("add_user_id");
		String date = request.getParameter("add_date");
		int count = Integer.parseInt(countstring.replaceAll(" ", ""));
		System.out.println("信息:" + bill_in_id+ "   " + good_id+ "   " + count+ "   " + user_id+ "   " + date);
		InputDao inDao = new InputDao();		
		int flag = 	inDao.AddInputInfo(bill_in_id, good_id, count, user_id, date);
		System.out.println(flag);
		int index = inDao.add_changeCnt(good_id, count);
		System.out.println(index);
		response.sendRedirect("Inputservlet?oper=input_caozuo");
		
	}

Dao层AddInputInfo方法:

public int AddInputInfo(String bill_in_id, String good_id, int count, String user_id, String date) {
		// TODO Auto-generated method stub
		Connection conn = null;
		Statement stmt = null;
		try {
			conn = JDBCUtils.getConnection();
			stmt = conn.createStatement();
			String sqlString = 
					"insert into input values ("+ bill_in_id + "," +
			         good_id +"," + count + "," + user_id + ",'"+
					 date + "');";
			System.out.println("入库信息录入sql:" + sqlString);
			int index = stmt.executeUpdate(sqlString);
			return index;
		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			JDBCUtils.release(stmt, conn);
			//System.out.println("已关闭连接");
		}
		return -1;
	}

另附Domain实体类:

package domain;

public class Input {

	private String bill_in_id;
	private String good_id;
	private int count;
	private String user_id;
	private String date;
	public String getBill_in_id() {
		return bill_in_id;
	}
	public void setBill_in_id(String bill_in_id) {
		this.bill_in_id = bill_in_id;
	}
	public String getGood_id() {
		return good_id;
	}
	public void setGood_id(String good_id) {
		this.good_id = good_id;
	}
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	public String getUser_id() {
		return user_id;
	}
	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	
	
	
}

你可能感兴趣的:(JavaWeb,jsp,servlet,mysql)