Spring框架针对dao层的jdbcTemplate操作crud之update修改数据库操作

使用jdbcTemplate

原理是把加载驱动Class.forName("com.mysql.jdbc.Driver");

和连接数据库Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/sw_database?user=root&password=root");

用一个对象完成DriverManagerDataSource dataSource=new DriverManagerDataSource();

package com.swift;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

@Component(value="jdbcTemplateDemo")
public class JdbcTemplateDemo {
    public boolean add() {
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        String sql="insert into sw_user(username,password) values(?,?)";
        int count=jdbcTemplate.update(sql, "doomsday","20171127");
        if(count==1) {
            return true;
        }
        return false;
        }
    
    public boolean update() {
        DriverManagerDataSource dataSource=new DriverManagerDataSource();
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/sw_database");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        String sql="update sw_user set password=? where username=?";
        int count=jdbcTemplate.update(sql,"lunchtime","doomsday");
        if(count!=0) {
            return true;
        }
        return false;
        }
    
}

修改数据库操作使用JdbcTemplate对象根据数据源直接使用update方法完成,比之前简便很多。

之前完成需按下边方法:

PreparedStatement ps=conn.prepareStatement("update sw_user set password=? where username=?");

ps.setString(1,“lunchtime”);

ps.setString(2, “doomsday”);

//ResultSet rs=ps.executeQuery();(不是这句,这句是select 语句的)

int count=ps.executeUpdate();

 if(count!=0) {return true;}

上边代码使用Spring框架注解生成对象方法

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:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
         http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!-- 开启注解扫描——对象和属性 -->
    <context:component-scan base-package="com.swift"></context:component-scan>
    <!-- 开启aop注解方法 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
    
</beans>

使用Servlet类进行测试:

package com.swift;

import java.io.IOException;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
@WebServlet("/test")
public class ServletTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public ServletTest() {
        super();
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().append("Served at: ").append(request.getContextPath());
        ApplicationContext context=new ClassPathXmlApplicationContext("aop.xml");
        JdbcTemplateDemo jdbcTemplateDemo=(JdbcTemplateDemo) context.getBean("jdbcTemplateDemo");
        if(jdbcTemplateDemo.update()) {
            response.getWriter().append("账号修改成功");
        }else {
            response.getWriter().append("修改失败");
        }
        
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

运行结果图

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值