Spring uses jdbctemplate to operate databases

Source: Internet
Author: User

Source: http://www.blogjava.net/baoyaer/articles/154080.html

First, assume that the following SQL table contains data username = test1, passwd = test1, address = test1

 

Create Table 'login '(

'Username' varchar (10) default null,

'Passwd' varchar (10) default null,

'Address' varchar (10) default null

) Engine = InnoDB default charset = gb2312;

 

Configuration file:

<? XML version = "1.0" encoding = "UTF-8"?>

<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">

<Beans>

<Bean id = "datasource" class = "org. Apache. commons. DBCP. basicdatasource">

<Property name = "driverclassname">

<Value> com. MySQL. JDBC. Driver </value>

</Property>

<Property name = "url">

<Value> JDBC: mysql: // localhost: 3306/javaee </value>

</Property>

<Property name = "username">

<Value> root </value>

</Property>

<Property name = "password">

<Value> 1234 </value>

</Property>

</Bean>

<Bean id = "jdbctemplate" class = "org. springframework. JDBC. Core. jdbctemplate">

<Property name = "datasource">

<Ref local = "datasource"/>

</Property>

</Bean>

 

<Bean id = "persondao" class = "springjdbcsupport. readdata. persondao">

<Property name = "jdbctemplate">

<Ref local = "jdbctemplate"/>

</Property>

</Bean>

</Beans>

 

JavaBean:

</P> <p> package springjdbcsupport. readdata; <br/> Import COM. mySQL. JDBC. driver; <br/> public class person {<br/> private string name; <br/> private string password; <br/> private string address; <br/> Public Person () {</P> <p >}< br/> Public Person (string name, string password, string address) {<br/> This. name = Name; <br/> This. password = password; <br/> This. address = address; <br/>}< br/> Public String geta Ddress () {<br/> return address; <br/>}< br/> Public void setaddress (string address) {<br/> This. address = address; <br/>}< br/> Public String getname () {<br/> return name; <br/>}< br/> Public void setname (string name) {<br/> This. name = Name; <br/>}< br/> Public String GetPassword () {<br/> return password; <br/>}< br/> Public void setpassword (string password) {<br/> This. password = password; <br/ >}< Br/> Public String tostring () {<br/> return this. getname () + "-" + this. getPassword () + "-" + this. getaddress (); <br/>}</P> <p> Dao: <br/> the getpersonbyrowcallbackhandler method obtains the person object based on username. <br/> package springjdbcsupport. readdata; <br/> Import Java. SQL. preparedstatement; <br/> Import Java. SQL. resultset; <br/> Import Java. SQL. sqlexception; <br/> Import Java. SQL. types; <br/> Import Java. util. list; <br/> Import Org. springframework. JDBC. core. batchpreparedstatementsetter; <br/> Import Org. springframework. JDBC. core. jdbctemplate; <br/> Import Org. springframework. JDBC. core. rowcallbackhandler; <br/> public class persondao {<br/> private jdbctemplate; <br/> Public jdbctemplate getjdbctemplate () {<br/> return jdbctemplate; <br/>}< br/> Public void setjdbctemplate (jdbctemplate) {<br /> This. jdbctemplate = jdbctemplate; <br/>}< br/> Public int insertpersonuseupdate (person) {<br/> string SQL = "insert into login values (?,?,?) "; <Br/> object [] Params = new object [] {<br/> person. getname (), <br/> person. getPassword (), <br/> person. getaddress () <br/>}; <br/> return this. getjdbctemplate (). update (SQL, Params); <br/>}< br/> Public int insertpersonuseexecute (person) {<br/> string SQL = "insert into login values (?,?,?) "; <Br/> object [] Params = new object [] {<br/> person. getname (), <br/> person. getPassword (), <br/> person. getaddress () <br/>}; <br/> int [] types = new int [] {<br/> types. varchar, <br/> types. varchar, <br/> types. varchar <br/>}; <br/> return this. getjdbctemplate (). update (SQL, Params, types); <br/>}< br/> Public int [] updatepersonusebatchupdate (final list persons) {<br/> string SQL = "insert into login Val Ues (?,?,?) "; <Br/> batchpreparedstatementsetter setter = NULL; <br/> setter = new batchpreparedstatementsetter () {<br/> Public int getbatchsize () {<br/> return persons. size (); <br/>}< br/> Public void setvalues (preparedstatement ps, int index) throws sqlexception {<br/> person = (person) persons. get (INDEX); <br/> ps. setstring (1, person. getname (); <br/> ps. setstring (2, person. getPassword (); <br/> ps. setstring (3, person. getaddress (); <br/>}< br/>}; <br/> return this. getjdbctemplate (). batchupdate (SQL, setter); <br/>}< br/> Public Person getpersonbyrowcallbackhandler (string username) {<br/> string SQL = "select * from login where username =? "; <Br/> final person = new person (); <br/> final object Params [] = new object [] {username}; <br/> This. getjdbctemplate (). query (SQL, Params, new rowcallbackhandler () {<br/> Public void processrow (resultset RS) throws sqlexception {<br/> person. setname (RS. getstring ("username"); <br/> person. setpassword (RS. getstring ("passwd"); <br/> person. setaddress (RS. getstring ("Address"); <br/>}< br/>}); <br/> return person; <br/>}</P> <p> test code: </P> <p> package springjdbcsupport. readdata; <br/> Import Java. io. file; <br/> Import Java. util. arraylist; <br/> Import Java. util. list; <br/> Import Org. springframework. beans. factory. beanfactory; <br/> Import Org. springframework. beans. factory. XML. xmlbeanfactory; <br/> Import Org. springframework. core. io. filesystemresource; <br/> public class testjdbctemplate {<br/> Public static string filepath = ""; <br/> Public static beanfactory factory = NULL; <br/> Public static void main (string [] ARGs) {<br/> filepath = system. getproperty ("user. dir ") + file. separator + "springjdbcsupport" + file. separator + "readdata" + file. separator + "hello. XML "; <br/> factory = new xmlbeanfactory (New filesystemresource (filepath); <br/> persondao = (persondao) factory. getbean ("persondao"); <br/>/* <br/> * prepare data <br/> */<br/> person p1 = new person ("test1 ", "test1", "test1"); <br/> person P2 = new person ("Test2", "Test2", "Test2 "); <br/> person P3 = new person ("test3", "test3", "test3"); <br/> person P4 = new person ("test4 ", "test4", "test4"); <br/> person P5 = new person ("test5", "test5", "test5 "); <br/> list persons = new arraylist (); <br/> persons. add (P3); <br/> persons. add (P4); <br/> persons. add (P5); <br/> // use jdbctemplate. update method <br/> // persondao. insertpersonuseupdate (P1); <br/> // use jdbctemplate.exe cute mode <br/> // persondao. insertpersonuseexecute (P2); <br/> /// use the jdbctemplate batch processing method <br/> // persondao. updatepersonusebatchupdate (persons); </P> <p> // use rowcallbackhandler to execute a query and print the person information. <br/> system. out. println (persondao. getpersonbyrowcallbackhandler ("test1"); <br/>}</P> <p> 

Running result:

Test1-test1-test1

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.