首先,我建立了如下的数据库:
表中用了10个量,然后建立了后台的javabean文件封装get set,前台两个jsp文件
后台的Javabean文件如下:
package com.gzw.javabean; public class javabean { private String name; private String factory; private String model; private String type; private String count; private String date; private String time; private String inputname; private String outputname; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getFactory() { return factory; } public void setFactory(String factory) { this.factory = factory; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getCount() { return count; } public void setCount(String count) { this.count = count; } public String getDate() { return date; } public void setDate(String date) { this.date = date; } public String getTime() { return time; } public void setTime(String time) { this.time = time; } public String getInputname() { return inputname; } public void setInputname(String inputname) { this.inputname = inputname; } public String getOutputname() { return outputname; } public void setOutputname(String outputname) { this.outputname = outputname; } public javabean() {} public javabean(String name, String factory, String model, String type,String count,String date, String time, String inputname,String outputname) { this.name = name; this.factory = factory; this.model = model; this.type = type; this.count = count; this.date = date; this.time = time; this.inputname = inputname; this.outputname = outputname; } }
前台界面效果如下:
数据库修改成功:
jsp代码如下:
houseadd.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>信息录入</title> </head> <body> <form action="housearesult.jsp" method="post"> <table align="center"> <tr> <th colspan="2" align="center" color="red"><h2>添加单据</h2></th> </tr> <tr> <td align="center" >商品名称:</td> <td align="left" ><input type="text" name="name"></td> </tr> <tr> <td align="center" >生产厂家:</td> <td align="left" ><input type="text" name="factory"></td> </tr> <tr> <td align="center" >型号:</td> <td align="left" ><input type="text" name="model"></td> </tr> <tr> <td align="center" >规格:</td> <td align="left" ><input type="text" name="type"></td> </tr> <tr> <td align="center" >数量:</td> <td align="left" ><input type="text" name="count"></td> </tr> <tr> <td align="center" >日期:</td> <td align="left" ><input type="text" name="date"></td> </tr> <tr> <td align="center" >时间:</td> <td align="left" ><input type="text" name="time"></td> </tr> <tr> <td align="center" >入库单位(或出库单位)名称:</td> <td align="left" ><input type="text" name="inputname"></td> </tr> <tr> <td align="center" >送货(或提货)人姓名:</td> <td align="left" ><input type="text" name="outputname"></td> </tr> <tr> <th colspan="2" align="center" ><input type="submit" name="submit" value="添加"></th> </tr> </table> </form> <h3 align="center"><input type="button" value="退出" onclick="window.close()"></h3> </body> </html>
housearesult.jsp <%@page import="java.sql.PreparedStatement"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@page import="java.sql.Connection" %> <%@page import="java.sql.DriverManager" %> <% request.setCharacterEncoding("UTF-8"); %> <jsp:useBean id="book" class="com.gzw.javabean.javabean"></jsp:useBean> <jsp:setProperty property="*" name="book"/> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>添加结果</title> </head> <body> <% try{ Class.forName("com.mysql.jdbc.Driver"); String url = "jdbc:mysql://localhost:3306/house"; String username = "root"; // 用户名 String password = "jinmaodadi668"; // 密码 Connection conn = DriverManager.getConnection(url, username, password); //连接数据库的 //INSERT INTO 语句用于向表格中插入新的行。 String sql = "insert into stu(name,factory,model,type,count,date,time,inputname,outputname)values(?,?,?,?,?,?,?,?,?)"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, book.getName()); ps.setString(2, book.getFactory()); ps.setString(3, book.getModel()); ps.setString(4, book.getType()); ps.setString(5, book.getCount()); ps.setString(6, book.getDate()); ps.setString(7, book.getTime()); ps.setString(8, book.getInputname()); ps.setString(9, book.getOutputname()); int row = ps.executeUpdate(); //executeUpdate()返回执行完成后有多少行数据受到影响 if(row > 0){ out.print("添加成功! "); } ps.close(); conn.close(); }catch(Exception e){ out.print("添加失败:" + e.toString()); } %> <h3 align="center"><a href = "houseadd.jsp">返回主菜单<br></a></h3> <h3 align="center"><input type="button" value="退出" onclick="window.close()"></h3> </body> </html>