Spring Learning -6-JDBC Support

Source: Internet
Author: User

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 "&GT;&LT;/PROPERTY&GT <property name= "Initialpoolsize" value= "3" ></property> <property name= "maxpoolsize" value= "&GT;&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" &GT;&LT;/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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.