Spring框架針對dao層的jdbcTemplate操作crud之add添加數據庫操作
阿新 • • 發佈:2017-11-25
classname charset xml配置 context 連接 void oot jdbc use
使用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 jdbcTemplate=new JdbcTemplate(dataSource); String sql="insert into sw_user(username,password) values(?,?)";int count=jdbcTemplate.update(sql, "doomsday","20171125"); if(count==1) { return true; } return false; } }
添加數據庫操作使用JdbcTemplate對象根據數據源直接使用update方法完成,比之前簡便很多。
之前完成需按下邊方法:
PreparedStatement ps=conn.prepareStatement("insert into sw_user(username,password) values(?,?)");
ps.setString(1,“doomsday”);
ps.setString(2, “20171125”);
//ResultSet rs=ps.executeQuery();(不是這句,這句是select 語句的)
int count=ps.executeUpdate();
if(count==1) {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" 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"> <!-- 開啟註解掃描 --> <context:component-scan base-package="com.swift"></context:component-scan> </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("/ServletTest") 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("beanZhujie.xml"); JdbcTemplateDemo jdbcTemplateDemo=(JdbcTemplateDemo) context.getBean("jdbcTemplateDemo"); if(jdbcTemplateDemo.add()) { response.getWriter().append("帳號註冊成功"); }else { response.getWriter().append("註冊失敗"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
瀏覽器中的到結果如下:
navicat premium 中成功添加數據,結果如下:
Spring框架針對dao層的jdbcTemplate操作crud之add添加數據庫操作