Spring uses JdbcTemplate and Jdbcdaosupport and Namedparameter

Source: Internet
Author: User

JdbcTemplate:

Connect the database First

<!--import external files--><context:property-placeholder location= "classpath:db.properties"/>    <!-- Configure C3P0 Data Source--    class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >        <property name= " Driverclass "value=" ${driverclass} "></property>        <property name=" Jdbcurl "value=" ${jdbcurl} "> </property>        <property name= "user" value= "${user}" ></property>        <property name= " Password "value=" ${password} "></property>            </bean>

External configuration

driverclass=oracle.jdbc.OracleDriverjdbcUrl=jdbc:oracle:thin:@192.168.1.105:1521: Orcluser= Emspassword=123456

Note that when writing the URL of the machine's IP, write localhost when the configuration of the service in the Oracle database to write localhost

In modifying

Code for the connection test

 Public classJdbctest {PrivateApplicationContext ctx=NULL; PrivateJdbcTemplate JdbcTemplate; {CTX=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); JdbcTemplate= (JdbcTemplate) ctx.getbean ("JdbcTemplate"); } @Test//Test Connection     Public voidTestdatasource ()throwssqlexception{DataSource DataSource=ctx.getbean (DataSource.class);    System.out.println (Datasource.getconnection ()); }    }

Five methods supported by JdbcTemplate:

    • Execute method: can be used to execute any SQL statement, generally used to execute DDL statements;

    • Update method and BatchUpdate method:Update method is used to execute new, modify, delete and other statements; The BatchUpdate method is used to execute batch-related statements;

    • Query method and Queryforxxx method: used to execute queries related to the statement;

    • Call method: used to execute stored procedures, function-related statements.

Configuring JdbcTemplate in XML

<!--Configure Spring JdbcTemplate--    class= "Org.springframework.jdbc.core.JdbcTemplate" >        <property name= "DataSource" ref= "DataSource" ></property>    </bean>

Use:

 PackageCom.spring.jdbc;Importjava.sql.SQLException;ImportJavax.sql.DataSource;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importorg.springframework.jdbc.core.JdbcTemplate; Public classJdbctest {PrivateApplicationContext ctx=NULL; PrivateJdbcTemplate JdbcTemplate; {CTX=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); JdbcTemplate= (JdbcTemplate) ctx.getbean ("JdbcTemplate"); } @Test//Test Connection     Public voidTestdatasource ()throwssqlexception{DataSource DataSource=ctx.getbean (DataSource.class);    System.out.println (Datasource.getconnection ()); } @Test Public voidtestupdate () {String SQL= "Update employees set last_name=?" where id=? "; Jdbctemplate.update (SQL,"Jack", 2); }}

Batch modification:

// Batch Modification     @Test    publicvoid  testbatchupdate () {        String sql= ' INSERT INTO Employees (Last_name,deptid) VALUES (?,?) " ;        List<Object[]> batchargs=new arraylist<object[]>();        Batchargs.add (new object[]{"AA", 1});        Batchargs.add (new object[]{"BB", 2});        Jdbctemplate.batchupdate (Sql,batchargs);    }

Namedparameterjdbctemplate: A named parameter that enables the appliance name parameter

Configuration Bean

<!--configuration Namedparameterjdbctemplate The object can make the appliance name parameter, which has no parameterless constructor, so you must specify the parameter---    class= "for the constructor" Org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate ">        <constructor-arg ref=" DataSource "></constructor-arg>    </bean>

Java testing:

 PackageCom.spring.jdbc;Importjava.sql.SQLException;Importjava.util.ArrayList;ImportJava.util.HashMap;Importjava.util.List;ImportJava.util.Map;ImportJavax.sql.DataSource;Importorg.junit.Test;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext;Importorg.springframework.jdbc.core.JdbcTemplate;Importorg.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; Public classJdbctest {PrivateApplicationContext ctx=NULL; Privatenamedparameterjdbctemplate namedparameterjdbctemplate; {CTX=NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); Namedparameterjdbctemplate=ctx.getbean (namedparameterjdbctemplate.class); } @Test Public voidtestnamedparameterjdbctemplate () {String SQL= "INSERT into employees (Id,last_name,email,deptid) VALUES (: A,:ln,:email,:id)"; Map<String,Object> parammap=NewHashmap<string, object>(); Parammap.put ("A", 3); Parammap.put ("LN", "FF"); Parammap.put ("Email", "[email protected]"); Parammap.put ("id", 2);    Namedparameterjdbctemplate.update (SQL, PARAMMAP); }}
Improved:
public void TestNamedParameterJdbcTemplate2 () {
String sql= "INSERT into employees (Id,last_name,email,deptid) VALUES (: Id,:lastname,:email,:d eptid)";
Employees employee=new Employees ();
Employee.setlastname ("haha");
Employee.setemail ("[email protected]");
Employee.setdeptid (5);
Sqlparametersource parametersource=new Beanpropertysqlparametersource (employee);
Namedparameterjdbctemplate.update (SQL, Parametersource);
}
The parameter name is the property of the object. The property is then passed in directly.

Spring uses JdbcTemplate and Jdbcdaosupport and Namedparameter

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.