java model1
goods.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<body>
<div style="margin:0 auto;">
<form action="service.jsp" method="post">
<table width="400px">
<caption>录入商品信息</caption>
<tr>
<td width="30%" align="right">商品名称:</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td width="30%" align="right">价格:</td>
<td><input type="text" name="price"></td>
</tr>
<tr>
<td width="30%" align="right">描述:</td>
<td><input type="text" name="description"></td>
</tr>
<tr>
<td width="30%" align="center">i<input type="submit" value="提交"></td>
<td></td>
</tr>
</table>
</form>
</div>
</body>
</html>
service.jsp
<%@ page language="java" contentType="text/html; charset=gb2312"
pageEncoding="gb2312"%>
<!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=gb2312">
<title>Insert title here</title>
</head>
<jsp:useBean id="Goods" class="com.Model.Goods"></jsp:useBean>
<jsp:setProperty property="*" name="Goods"/>
<jsp:useBean id="GoodsDao" class="com.dao.GoodsDao"></jsp:useBean>
<%
GoodsDao.saveGoods(Goods);
%>
</body>
</html>
filter.java
package com.util;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* Servlet Filter implementation class CharacterEncodingFilter
*/
public class CharacterEncodingFilter implements Filter {
protected String encoding=null;
protected FilterConfig filterConfig=null;
public CharacterEncodingFilter() {
}
public void destroy() {
this.encoding=null;
this.filterConfig=null;
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
if(this.encoding!=null)
{
request.setCharacterEncoding(encoding);
response.setContentType("text/html;charset="+this.encoding);
}
chain.doFilter(request, response);
}
/**
* @see Filter#init(FilterConfig)
*/
public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig=fConfig;
this.encoding=filterConfig.getInitParameter("encoding");
}
}
Goods.java
package com.Model;
public class Goods {
private String name;
private double price;
private String description;
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double d) {
this.price = d;
}
}
GoodsDao.java
package com.dao;
import java.sql.*;
import java.sql.Connection;
public class GoodsDao {
public void saveGoods(com.Model.Goods goods)
{
System.out.println("name:"+goods.getName()+"price:"+goods.getPrice()+"desciption:"+goods.getDescription());
try
{ try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
}
catch(Exception e)
{
System.out.print(e.getMessage()+"nihao");
}
String url="jdbc:sqlserver://localhost:1433;databaseName=user_login";
Connection conn=DriverManager.getConnection(url,"du","123");
String sqlstr="insert into good values(?,?,?)";
System.out.print(sqlstr);
PreparedStatement ps=conn.prepareStatement(sqlstr);
System.out.println("nihao");
ps.setString(1,goods.getName().trim());
ps.setDouble(2, goods.getPrice());
ps.setString(3, goods.getDescription().trim());
System.out.println(ps.toString());
ps.executeUpdate();
ps.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
浙公网安备 33010602011771号