如何取数据五 spring+servlet(request取数据,然后setget dao)+jdbc存数据库

spring 好像变成了数据库的链接工作,spring真真的作用是什么?【这个例子没有体现出来,spring的作用是策略模式,根据不同的配置,applicationContext.xml 去不同的bean 类处理。如中文说话和english说话的类,控制反转】

1、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"
	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/tcxb?characterEncoding=UTF-8
			</value>
		</property>
		<property name="username">
			<value>root</value>
		</property>
		<property name="password">
			<value>admin</value>
		</property>
	</bean>
	<!-- 为UserDAO注入数据源 -->
	<bean id="userDAO" class="com.mr.dao.UserDAO">
		<property name="dataSource">
			<ref local="dataSource"/>
		</property>
	</bean>
</beans>

2、前端还是那个界面。

action="springdaomysql" 

3、web.xml

   <servlet>
		<servlet-name>springdaomysqldemo</servlet-name>
		<servlet-class>com.mr.dao.springjdbcdaomysql</servlet-class>
	</servlet>
	<servlet-mapping>
		<servlet-name>springdaomysqldemo</servlet-name>
		<url-pattern>/springdaomysql</url-pattern>
	</servlet-mapping>

4、springjdbcdaomysql.java

package com.mr.dao;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import com.mr.user.User;
import com.mysql.jdbc.PreparedStatement;

public class springjdbcdaomysql extends HttpServlet {
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
	 Resource resource = new ClassPathResource("applicationContext.xml");            //装载配置文件
		 
		  BeanFactory factory = new XmlBeanFactory(resource);
	    
	     
	        UserDAO userDAO = (UserDAO) factory.getBean("userDAO");//获取UserDAO 配置文件里的userDao

		
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
			String xsxm = request.getParameter("entry[field_9]");
		String jzxm = request.getParameter("entry[field_17]");
		String old = request.getParameter("entry[field_18]");
		String nianji = request.getParameter("entry[field_7]");
		String iphone = request.getParameter("entry[field_16]");
	    User user = new User();//实例化User对象
        user.setXsxm(xsxm);
        user.setJzxm(jzxm);
        user.setOld(old);
        user.setNianji(nianji);
        user.setIphone(iphone);
        
        
        userDAO.inserUser(user);//执行添加方法,数据库语句执行在这个类里面的方法
        System.out.println("数据添加成功!!!");
          
        
	}
}

UserDAO.java

package com.mr.dao;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

import javax.sql.DataSource;

import com.mr.user.User;

public class UserDAO implements UserDAOImpl {
    private DataSource dataSource;//注入DataSource
    public DataSource getDataSource() {
        return dataSource;
    }
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }
    //向数据表tb_user中添加数据
    public void inserUser(User user) {
        String xsxm = user.getXsxm();//获取学生姓名
        String jzxm = user.getJzxm();//获取家长姓名
        String old = user.getOld();//获取年龄
        String nianji = user.getNianji();//获取年纪
        System.out.println(nianji);
        String iphone = user.getIphone();//获取电话
        Connection conn = null;//定义Connection
        Statement stmt = null;//定义Statement   
         try {
             conn = dataSource.getConnection();//获取数据库连接
             stmt = conn.createStatement();
      
             stmt.execute("insert into baoming(xsxm,jzxm,old,nianji,iphone) " 
                  + "VALUES('"+xsxm+"','" + jzxm + "','" + old + "','" + nianji + "','" + iphone + "')");//添加数据的SQL语句
         } catch (SQLException e) {
             e.printStackTrace();
         }
         finally {
             if(stmt != null) {
                 try {
                     stmt.close();//关闭Statement对象
                 }   
                 catch(SQLException e) {
                     e.printStackTrace();
                 }
             }
             if(conn != null) {
                 try {
                     conn.close();//关闭数据库连接
                 }
                 catch(SQLException e) {
                     e.printStackTrace();
                 }
             }
         }
    }
}


UserDAOImpl.java

package com.mr.dao;

import com.mr.user.User;

public interface UserDAOImpl {
    public void inserUser(User user);//添加用户信息的方法
}



展开阅读全文
©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值