JdbcTemplate is the spring provides a simplified JDBC development Template Tool class. In order to better understand the entire JdbcTemplate configuration database connection pool process, this article does not adopt the configuration file way, but uses the most basic code
Way to write. The latter article will tell you how to configure the file.
1.Spring a bit of support for persistence layer
2.jdbcTemplate QuickStart First step: Importing a jar package into a project where the spring core package has four, the log packet has two, the test package one. The following two jar packages are also imported because of the need to use JdbcTemplate
Spring-jdbc-3.2.0.release.jar
Spring-tx-3.2.0.release.jar
Because you want to use the MySQL database package, then import the MySQL package
Step two: Write the JDBC template program here, so to speak: Drivermanagerdatasource is Spring's built-in database connection pool, which is the same as the c3p0 level. JdbcTemplate is a class that spring provides for easy database operation. Inside is the database connection pool to pass in it. The JdbcTemplate constructor is
Public JdbcTemplate (DataSource DataSource) {
This means that any class that inherits the database interface can be passed in. For example, JdbcTemplate (where the C3P0 database connection pool is placed) is also possible. In other words, the first and second steps of the following code are not very related. It's universal.
The specific cases are as follows:
Public voidTestdemo1 () {//use JdbcTemplate to build a table//1. Create a database connection pool, using spring's built-in connection poolDrivermanagerdatasource datasource=NewDrivermanagerdatasource (); //Connecting Database DriversDatasource.setdriverclassname ("Com.mysql.jdbc.Driver"); Datasource.seturl ("Jdbc:mysql:///spring3_day2"); Datasource.setusername ("Root"); Datasource.setpassword ("Root");
//2. Constructing a template object from a connection poolJdbcTemplate jdbctemplate=NewJdbcTemplate (DataSource); //3. Execute the SQL statement, create a person table, set the fields insideJdbctemplate.execute ("CREATE TABLE person (ID int primary key,name varchar (20))"); }
You can view the source code of the JdbcTemplate.
The program execution results are in spring3_day2 creates a person table,
20spring_jdbctemplatem Template Tool Class