GoodsInfo.java:
package com.lh.bean;
public class GoodsInfo {
private int id;
private String name;
private float price;
private String type;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
}
GoodsDao.java:
package com.lh.dao;
import com.lh.bean.GoodsInfo;
public interface GoodsDao {
public void addGoods(GoodsInfo goods);
}
GoodsDaoImpl.java:
package com.lh.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import javax.sql.DataSource;
import com.lh.bean.GoodsInfo;
import com.lh.dao.GoodsDao;
public class GoodsDaoImpl implements GoodsDao {
private DataSource dataSource;
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void addGoods(GoodsInfo goods) {
Connection conn=null;
PreparedStatement stmt=null;
try{
conn = dataSource.getConnection();
String sql = "insert into tb_goods(name,price,type) values(?,?,?);";
stmt = conn.prepareStatement(sql);
stmt.setString(1, goods.getName());
stmt.setFloat(2, goods.getPrice());
stmt.setString(3, goods.getType());
stmt.executeUpdate();
}catch(Exception ex){
ex.printStackTrace();
}
finally{
try {
stmt.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url" >
<value>jdbc:mysql://localhost:3306/test</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>001052</value>
</property>
</bean>
<bean id="goodsDao" class="com.lh.dao.impl.GoodsDaoImpl">
<property name="dataSource">
<ref local="dataSource"/>
</property>
</bean>
</beans>
index.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>应用Spring Dao添加商品信息</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<form action="save.jsp" method="post">
<table>
<tr>
<td>商品名称:</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>商品价格:</td>
<td>
<input type="text" name="price" />
</td>
</tr>
<tr>
<td>商品类别:</td>
<td>
<input type="text" name="type" />
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="添加到数据库" />
</td>
</tr>
</table>
</form>
</body>
</html>
save.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="org.springframework.core.io.*"%>
<%@ page import="org.springframework.beans.factory.BeanFactory"%>
<%@ page import="org.springframework.beans.factory.xml.XmlBeanFactory"%>
<%@ page import="com.lh.dao.impl.*"%>
<%@ page import="com.lh.bean.GoodsInfo"%>
<%
request.setCharacterEncoding("GBK");
String name = request.getParameter("name");
String price = request.getParameter("price");
String type = request.getParameter("type");
GoodsInfo goods = new GoodsInfo();
goods.setName(name);
goods.setPrice(Float.parseFloat(price));
goods.setType(type);
Resource resource = new ClassPathResource("applicationContext.xml");
BeanFactory factory = new XmlBeanFactory(resource);
GoodsDaoImpl dao = (GoodsDaoImpl)factory.getBean("goodsDao");
dao.addGoods(goods);
out.println("<script type='text/javascript'> alert('添加成功!');window.location.href='index.jsp'</script>");
%>