Integrate JDBC with spring

Source: Internet
Author: User

Use the three JDBC template classes (JdbcTemplate, NamedParameterJdbcTemplate, and SimpleJdbcTemplate) provided by Spring to operate the database.
1. JdbcTemplate is the most basic JDBC template in Spring. You can use JDBC and simple index parameter queries to perform simple database access.
2. NamedParameterJdbcTemplate can bind the value to the name parameter in the SQL statement during query, instead of the index parameter.
NamedParameterJdbcTemplate contains a JdbcTemplate, so all the tasks that JdbcTemplate can do are NamedParameterJdbcTemplate;
Compared with JdbcTemplate, NamedParameterJdbcTemplate mainly adds the parameter naming function.
Iii. SimpleJdbcTemplate simplifies the use of JDBC templates by leveraging Java 5 features, such as automatic packing, common and variable parameter lists
SimpleJdbcTemplate contains a NamedParameterJdbcTemplate. Therefore, NamedParameterJdbcTemplate can do all the things SimpleJdbcTemplate can do,
Compared with NamedParameterJdbcTemplate, SimpleJdbcTemplate mainly supports generic and variable-length JDK5.0 parameters.


1. Use JDBC Template


JdbcTemplate is a helper class that encapsulates JDBC operations. It is very easy to directly use JdbcTemplate ~ Template only depends on the data source.
The DriverManagerDataSource used in this example is a "pseudo" data source. It only simulates the form and does not implement the data source function.
DriverManagerDataSource ds = new DriverManagerDataSource ();
Ds. setDriverClassName ("com. mysql. jdbc. Driver ");
Ds. setUrl ("jdbc: mysql: // localhost: 3306/sampledb ");
Ds. setUsername ("root ");
Ds. setPassword ("");

JdbcTemplate jdbc = new JdbcTemplate ();
Jdbc. setDataSource (ds );
String SQL = "create table t_user1 (user_id int primary key, user_name varchar (60 ))";
Jdbc.exe cute (SQL );


2. JdbcDaoSupport, & Change Spring JDBC to "Spring"
If the JdbcTemplate is repeatedly declared in the code, the code will be seriously polluted. It is no different from JDBC ~
Therefore, Spring provides JdbcDaoSupport. If DAO inherits this class, JdbcTemplate is automatically obtained (provided that DataSource is injected ).


In addition, Spring's xml configuration can be well used here. The basic process is as follows:
(1) Declare the DataSrouce Bean. BasicDataSource is used here, which is the DBCP data source (the one used for Tomcat)
(2) Declare the Bean of the JdbcTemplate template and inject the DataSource
(3) Declare Dao, class is something integrated from JdbcDaoSupport, and inject JdbcTemplate.


The above Bean generation process can use Spring IDE ...... It is quite convenient. Code.


ApplicationContext. xml




Xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">



Class = "org. apache. commons. dbcp. BasicDataSource" abstract = "false"
Lazy-init = "default" autowire = "default" dependency-check = "default"
Destroy-method = "close">

Com. mysql. jdbc. Driver


Jdbc: mysql: // localhost: 3306/sampledb


Root






















Abstract = "false" lazy-init = "default" autowire = "default"
Dependency-check = "default">



ForumDAO
Package dao. jdbc;
Import org. springframework. jdbc. core. support. JdbcDaoSupport;
Public class ForumDAO extends JdbcDaoSupport
{
Public void initDb ()
{
String SQL = "create table t_user1 (user_id int primary key, user_name varchar (60 ))";
Getjdbctemplate(cmd.exe cute (SQL );
}
}


Main
Import org. springframework. jdbc. core. JdbcTemplate;
Import org. springframework. jdbc. datasource. DriverManagerDataSource;


Public class Main
{
Public static void main (String args [])
{
DriverManagerDataSource ds = new DriverManagerDataSource ();
Ds. setDriverClassName ("com. mysql. jdbc. Driver ");
Ds. setUrl ("jdbc: mysql: // localhost: 3306/sampledb ");
Ds. setUsername ("root ");
Ds. setPassword ("");

JdbcTemplate jdbc = new JdbcTemplate ();
Jdbc. setDataSource (ds );
String SQL = "create table t_user1 (user_id int primary key, user_name varchar (60 ))";
Jdbc.exe cute (SQL );
}


}

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.