20spring_jdbctemplatem Template Tool Class

Source: Internet
Author: User

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

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.