Spring 单独完成数据输入

 

                     

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>");
%>


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值