(7) Spring support for JDBC

Source: Internet
Author: User
Tags aop


Steps for using spring JDBC

1. Introduction of JAR Package

Spring-jdbc-3.2.5.release.jar

Spring-tx-3.2.5.release.jar

C3p0-0.9.1.2.jar

Mysql-connector-java-5.1.38-bin.jar

2. Configuration

C3P0 configuration of the database connection pool

JdbcTemplate Object

DAO object

3. Testing


Applicationcontext.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: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     &Nbsp;  http://www.springframework.org/schema/aop/spring-aop.xsd "><!-- 1.  Data source objects: &NBSP;C3P0 Connection Pool  --><bean id= "DataSource"  class= "Com.mchange.v2.c3p0.ComboPooledDataSource" ><property name= "Driverclass"  value= "Com.mysql.jdbc.Driver" ></property><property  name= "Jdbcurl"  value= "Jdbc:mysql://127.0.0.1:3306/test" ></property><property name= "User"  value= "root" ></property><property name= "password"  value= "root" ></ Property><property name= "Initialpoolsize"  value= "3" ></property><property name = "Maxpoolsize"  value= "ten" ></property><property name= "Maxstatements"  value= ">" </property><property name= "Acquireincrement"  value= "2" ></property></bean> <!-- 2.  Create JdbcTemplate object  --><bean id= "JdbcTemplate"  class= " Org.springframework.jdbc.core.JdbcTemplate "><constructor-arg index= "0"  type= "javax.sql.DataSource"  ref= "DataSource" ></ constructor-arg></bean><!-- dao  Instances  --><bean id= "Deptdao"  class= " Com.rk.hibernate.i_jdbc. Departmentdao "><property name=" JdbcTemplate " ref=" JdbcTemplate "></property></bean ></beans>

Department.java

Package Com.rk.hibernate.i_jdbc;public class Department{private int deptid;private String deptname;private int version; 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;} public int getversion () {return version;} public void setversion (int version) {this.version = version;} @Overridepublic String toString () {return "Department [deptid=" + DeptID + ", deptname=" + Deptname + ", version=" + versio n + "]";}}

Departmentdao.java

Package com.rk.hibernate.i_jdbc;import java.sql.resultset;import java.sql.sqlexception;import  java.util.List;import org.springframework.jdbc.core.JdbcTemplate;import  org.springframework.jdbc.core.rowmapper;public class departmentdao{private jdbctemplate  Jdbctemplate;public void setjdbctemplate (jdbctemplate jdbctemplate) {this.jdbcTemplate =  jdbctemplate;} Public void save () {string sql =  "Insert into t_department (name,dept_version )  values (?,?) "; Jdbctemplate.update (sql,  "Tom", 0);} Public department findbyid (int id) {string sql =  "SELECT * FROM T_ Department where id=? "; List<department> list = jdbctemplate.query (Sql,new myresult (), id);return  (list  != null && list.size () >0)  ? list.get (0)  : null;} Public list<department> findall() {string sql =  "select * from t_department"; List<department> list = jdbctemplate.query (Sql, new myresult ());return  List;}} class myresult implements rowmapper<department>{@Overridepublic  Department  Maprow (Resultset rs, int rownum)  throws SQLException{int id =  Rs.getint ("id"); String name = rs.getstring ("name"); Int dept_version = rs.getint ("Dept_version") ;D epartment dept = new department ();d Ept.setdeptid (ID);d ept.setdeptname (name); Dept.setversion (dept_version); return dept;}}

App.java

Package Com.rk.hibernate.i_jdbc;import Java.util.list;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;public class App{private ApplicationContext ac = New Classpathxmlapplicationcontext ("/com/rk/hibernate/i_jdbc/applicationcontext.xml"); @Testpublic void Test () { Departmentdao Deptdao = (Departmentdao) ac.getbean ("Deptdao");//1. Save//deptdao.save ();//2. Query by ID//department dept = deptdao.findbyid (9);//system.out.println (dept)//3. Querying all Lists <Department> list = Deptdao.findall (); System.out.println (List.size ()); SYSTEM.OUT.PRINTLN (list);}}











(7) Spring support for JDBC

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.