Using jdbcTemplate
The principle is to load the driver Class.forName("com.mysql.jdbc.Driver").
Connect to the database Connection conn = DriverManager. getConnection ("jdbc: mysql://localhost: 3306/sw_database? User = root & password = root");
Complete DriverManagerDataSource dataSource=new DriverManagerDataSource() with an object;
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 delete() {
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="delete from sw_user where username=?";
int count=jdbcTemplate.update(sql, "doomsday");
if(count!=0) {
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;
}
}
The deletion of database operations is done by using JdbcTemplate objects directly using the update method based on the data source, which is much simpler than before.
Before completion, press the following method:
PreparedStatement ps=conn.prepareStatement("delete from sw_user where username=?");
ps.setString(1,"doomsday");
// ResultSet rs=ps.executeQuery(); (Not this sentence, this sentence is the select statement)
int count=ps.executeUpdate();
if(count!=0) {return true;}
The code above uses Spring framework annotations to generate object methods
The xml configuration file code is as follows:
<?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"> <!-- Open Annotation Scan - Objects and Properties --> <context:component-scan base-package="com.swift"></context:component-scan> <!-- open aop Annotation method --> <aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>
Testing using the Servlet class:
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.delete()) {
response.getWriter().append("Successful account modification");
}else {
response.getWriter().append("Modification failed");
}
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
Operation Result Diagram