The Spring framework uses the jdbcTemplate operation on the dao layer to delete data in crud, and jdbctemplatecrud
Use jdbcTemplate
The principle is to load the Driver Class. forName ("com. mysql. jdbc. Driver ");
And Connection conn = DriverManager. getConnection ("jdbc: mysql: // localhost: 3306/sw_database? User = root & password = root ");
Use an object to complete DriverManagerDataSource = 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 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 delete database operation uses the JdbcTemplate object to directly use the update method based on the data source, which is much easier than before.
To complete the preceding steps, follow these steps:
PreparedStatement ps = conn. prepareStatement ("delete from sw_user where username =? ");
Ps. setString (1, "doomsday ");
// ResultSet rs=ps.exe cuteQuery (); (this sentence is not from the select statement)
Int count0000ps.exe cuteUpdate ();
If (count! = 0) {return true ;}
The above Code uses the Spring framework annotation to generate an object Method
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 "> <! -- Enable annotation scanning -- Object and attribute --> <context: component-scan base-package = "com. swift"> </context: component-scan> <! -- Enable the aop annotation Method --> <aop: aspectj-autoproxy> </beans>
Use the Servlet class for testing:
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 :"). append (request. getContextPath (); ApplicationContext context = new ClassPathXmlApplicationContext ("aop. xml "); JdbcTemplateDemo jdbcTemplateDemo = (JdbcTemplateDemo) context. getBean ("jdbcTemplateDemo"); if (jdbcTemplateDemo. delete () {response. getWriter (). append ("account modified successfully");} else {response. getWriter (). append ("failed to modify") ;}} protected void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {doGet (request, response );}}
Running result Diagram