Use the three JDBC template classes (JdbcTemplate, namedparameterjdbctemplate, simplejdbctemplate) that spring provides to manipulate the database
First, JdbcTemplate is the most basic JDBC template in spring, using JDBC and simple index parameter query to make simple access to the database
Second, namedparameterjdbctemplate can bind the value to the named parameter in SQL instead of the index parameter when querying.
Namedparameterjdbctemplate interior contains a jdbctemplate, so jdbctemplate can do things namedparameterjdbctemplate are capable;
Namedparameterjdbctemplate relative to JdbcTemplate mainly adds the function that the parameter can be named.
Simplejdbctemplate uses JAVA5 features such as automatic boxing, generic, and variable parameter lists to simplify the use of JDBC templates
The simplejdbctemplate contains a namedparameterjdbctemplate; so namedparameterjdbctemplate can do things simplejdbctemplate are capable of,
Simplejdbctemplate mainly increases JDK5.0 and variable-length parameter support relative to Namedparameterjdbctemplate.
1. Use JDBC Template
JdbcTemplate is a helper class that encapsulates the operation of JDBC, and it is simple to use JdbcTemplate directly ~template depend only on the data source.
This example uses the Drivermanagerdatasource, is a "pseudo" data source, just simulates the form and does nothing to 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)";
Jdbc.execute (SQL);
2, jdbcdaosupport,&& let spring jdbc more "Spring"
If you repeatedly declare jdbctemplate in your code, the code will be contaminated very seriously, it's no different from JDBC.
So spring provides jdbcdaosupport, and all DAO inherits this class, and it automatically gets jdbctemplate (provided it is injected DataSource).
In addition: Spring's XML configuration can be well used here, in XML configuration, the basic process is as follows:
(1) Declaring the Datasrouce bean, here with Basicdatasource, is the DBCP data source (the one for Tomcat)
(2) Declaring the template JdbcTemplate bean and injecting datasource into the
(3) Declare Dao,class as an integrated jdbcdaosupport, and inject JdbcTemplate.
The above bean generation process can be used with the spring IDE ... Quite convenient. Don't say anything else, Code.
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/
Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "http://www.springframework.org/schema/ Beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd "> <bean id=" DataSource "CLA ss= "Org.apache.commons.dbcp.BasicDataSource" abstract= "false" lazy-init= "default" autowire= "Default" dependency-check= "Default" destroy-method= "Close" > <property name= "driverclassname" > <value>c om.mysql.jdbc.driver</value> </property> <property name= "url" > <value>jdbc:mysql:/ /localhost:3306/sampledb</value> </property> <property name= "username" > <value>roo
t</value> </property> <property name= "password" > <null/> </property> </bean> <bean id= "JdbcTemplate" CLAss= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "DataSource" > <ref bean= "DataSource" "/> </property> </bean> <bean id=" Dao "abstract=" true "> <property na Me= "JdbcTemplate" > <ref bean= "jdbctemplate"/> </property> </bean> < Bean id= "Forumdao" class= "Dao.jdbc.ForumDAO" parent= "JdbcTemplate" abstract= "false" lazy-init= "Default" autowire= "
Default "dependency-check=" "Default" > </bean></beans> Forumdao Package dao.jdbc;
Import Org.springframework.jdbc.core.support.JdbcDaoSupport; public class Forumdao extends Jdbcdaosupport {public void Initdb () {String sql = ' Create Tabl
e t_user1 (user_id int primary key,user_name varchar (60)) ";
Getjdbctemplate (). Execute (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 Drivermanagerdat
Asource ();
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.execute (SQL); }
}
Author: csdn blog Bujikuibu Small stream