核心代码:
insert.jsp
<body>
<form action="pre_insert.jsp" method="post">
商品编号:<input type="text" name="p_id"/><br>
商品类别:<input type="text" name="p_type"/><br>
商品名称:<input type="text" name="p_name"/><br>
商品价格:<input type="text" name="p_price"/><br>
商品数量:<input type="text" name="p_quantity"/><br>
图片路径:<input type="text" name="p_image"/><br>
商品描述:<input type="text" name="p_description"/><br>
<input type="submit" value="添加"/>
</form>
</body>
do_inset.jsp
<body>
<%
String JDriver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
String connectDB = "jdbc:sqlserver://127.0.0.1:1433;DatabaseName=ShopSystem";
try{
Class.forName(JDriver);
}catch(ClassNotFoundException e){
System.out.println("加载数据库引擎失败");
System.exit(0);
}
try{
String user = "sa";
String password = "xuelong";
Connection con = DriverManager.getConnection(connectDB, user, password);
System.out.println("数据库连接成功");
Statement stmt = con.createStatement();
//设置编码格式,处理中文乱码
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");
String p_id=request.getParameter("p_id");
String p_type=request.getParameter("p_type");
String p_name=request.getParameter("p_name");
float p_price=Float.parseFloat(request.getParameter("p_price"));
int p_quantity=Integer.parseInt(request.getParameter("p_quantity"));
String p_image=request.getParameter("p_image");
String p_description=request.getParameter("p_description");
//获取时间
Date date = new Date();
SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd");
String p_time=time.format(date);
/* 插入数据
String strSql="insert into product values('"+p_id+"','"+p_type+"','"+p_name+"','"+p_price+"','"+p_quantity+"','"+
p_image+"','"+p_description+"','"+p_time+"')";*/
/* 根据编号删除数据
String strSql="delete product where p_id='"+p_id+"'"; */
//修改数据
String strSql="update product set p_type='"+p_type+"',p_name='"+p_name+"',p_price='"+p_price+"',p_quantity='"+
p_quantity+"',p_image='"+p_image+"',p_description='"+p_description+"',p_time='"+p_time+"' where p_id='"+p_id+"'";
int intTemp=stmt.executeUpdate(strSql);
if(intTemp!=0){
out.println("商品添加成功!");
}else{
out.println("商品添加失败!");
}
stmt.close();
con.close();
}catch(Exception e){
out.println(e.toString());
}
%>
</body>