Spring provides a good support for JDBC technology
1) Spring support for C3P0 connection pool is perfect
2) Spring provides jdbctemplate to JDBC to simplify the JDBC operation
JdbcTemplate Template Tool class, similar to the Dbutils component
Steps to use:
1. Find the relevant JAR file
Dept
Package com.cx.jdbc;/** * Created by Cxspace on 16-8-10. */public class Dept { private int deptid; Private String deptname; public int Getdeptid () { return deptid; } public void Setdeptid (int deptid) { this.deptid = DeptID; } Public String Getdeptname () { return deptname; } public void Setdeptname (String deptname) { this.deptname = Deptname; }}
Com.cx.jdbc.UserDao01
Package Com.cx.jdbc;import Org.springframework.jdbc.core.jdbctemplate;import Org.springframework.jdbc.core.rowmapper;import Java.sql.resultset;import Java.sql.sqlexception;import java.util.list;/** * Created by Cxspace on 16-8-10. */public class UserDao01 {//Direct injection jdbctemplate private jdbctemplate jdbctemplate; public void Setjdbctemplate (JdbcTemplate jdbctemplate) {this.jdbctemplate = JdbcTemplate; } public void Save () {String-sql = "INSERT into t_dept (deptname) VALUES (' test01 ');"; Simplifying operations with the JDBC template jdbctemplate.update (SQL); } public void Deletebyid (int id) {String sql = ' Delete from t_dept where deptid =? '; Jdbctemplate.update (Sql,id); } public Dept FindByID (int id) {String sql = ' select * from t_dept WHERE deptid =? '; list<dept> list = jdbctemplate.query (sql, New Myresult (), id); Return (List!=null&&list.size () >0? List.get (0): null); } public list<dept> GetAll () { String sql = "SELECT * from T_dept"; list<dept> list = Jdbctemplate.query (Sql,new myresult ()); return list; } class Myresult implements rowmapper<dept>{@Override public Dept maprow (ResultSet ResultSet, int i) throws SQLException {Dept Dept = new Dept (); Dept.setdeptid (Resultset.getint ("DeptID")); Dept.setdeptname (resultset.getstring ("Deptname")); return dept; } }}
Com/cx/jdbc/bean.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:p= "http://www.springframework.org/schema/p" Xmlns:contex t= "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/con Text Http://www.springframework.org/schema/context/spring-context.xsd > <!--Data source object C3P0 connection pool-<bean Id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value= "COM.M Ysql.jdbc.Driver "></property> <property name=" Jdbcurl "value=" Jdbc:mysql:///learnhibernate "></ property> <property name= "user" value= "root" ></property> <property name= "password" value= " 33269456.cx "></PROPERTY> <property name= "Initialpoolsize" value= "3" ></property> <property name= "maxpoolsize" value= ">&L" t;/property> <property name= "maxstatements" value= "></property> <property name=" Acquir Eincrement "value=" 2 "></property> </bean> <!--create JdbcTemplate objects--<bean id=" JdbcTemplate " class= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "DataSource" ></PR Operty> </bean> <!--DAO Instance--<bean id= "Userdao" class= "com.cx.jdbc.UserDao01" > <pro Perty name= "JdbcTemplate" ref= "JdbcTemplate" ></property> </bean></beans>
Com.cx.jdbc.App
Package Com.cx.jdbc;import Org.junit.test;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * Created by Cxspace on 16-8-10. */public class App { //Container object applicationcontext ac = new Classpathxmlapplicationcontext ("Com/cx/jdbc/bean.xml" ); @Test public void TestApp () throws exception{ UserDao01 Userdao = (UserDao01) ac.getbean ("Userdao"); System.out.println (Userdao.findbyid (3)); System.out.println (Userdao.getall ()); Userdao.save (); Userdao.deletebyid (9);} }
Spring Learning -6-JDBC Support