A simple example of spring's JDBC (non-web program)

Source: Internet
Author: User

The first step:

Spring configuration Applicationcontext.xml file, placed under SRC:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE Beans Public "-//spring//dtd bean//en" "Http://www.springframework.org/dtd/spring-beans.dtd "><Beans>    <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource"Destroy-method= "Close">       //  < Propertyname= "Driverclassname"value= "Oracle.jdbc.driver.OracleDriver"/>     < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>     // < Propertyname= "url"value= "Jdbc:oracle:thin: @localhost: 1521:db10g"/>      < Propertyname= "url"value= "Jdbc:mysql://localhost:3306/iminer"/>        < Propertyname= "username"value= "Test"/>        < Propertyname= "Password"value= "pwd"/>    </Bean>    <BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate">       < Propertyname= "DataSource">           <refBean= "DataSource" />       </ Property>    </Bean>    <BeanID= "Jdbcutil"class= "Com.maggie.util.JdbcUtil">       < Propertyname= "JdbcTemplate">           <refBean= "JdbcTemplate" />       </ Property>    </Bean></Beans>

----------------------------------------------------------------------------------------

Step two:Com.maggie.util.JdbcUtil.java class 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.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method= "Close" >//<property name= "Driverclassname" value= "Oracle.jdbc.driver.OracleDriver"/><property name= "Driverclassname" value= "Com.mysql.jdbc.Driver"/>//<property name= "url" value= "Jdbc:oracle:thin: @localhost: 1521:db10g"/><property name= "url" value= "Jdbc:mysql://localhost:3306/iminer"/> <property name= "username" value= "test"/&        Gt <property name= "password" value= "pwd"/> </bean> <bean id= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "DataSource" > <ref bean= "Datasou Rce "/> </property> </bean> <bean id=" Jdbcutil "class= "Com.maggie.util.JdbcUtil" > <property name= "jdbctemplate" > <ref bean= "JdbcTemplate"/> </property> </bean></beans>

----------------------------------------------------------------------------------------

Step Three: Test the class:

 Packagecom.maggie.test;ImportJava.util.Map;Importorg.springframework.beans.factory.xml.XmlBeanFactory;ImportOrg.springframework.core.io.ClassPathResource;ImportCom.maggie.util.JdbcUtil; Public classtest{Privatejdbcutil jdbc;  PublicTest () {Classpathresource res=NewClasspathresource ("Applicationcontext.xml"); Xmlbeanfactory Factory=Newxmlbeanfactory (RES); JDBC= (jdbcutil) factory.getbean ("Jdbcutil"); }        Publicstring Getusernamebyid (string id) {Map dataname=jdbc.getjdbctemplate (). queryForMap ("Select T.username from tablename t where t.id=?",Newobject[] {ID}); return(String) Dataname.get ("username"); }        Public Static voidMain (string[] args) {test test=NewTest (); String ID= "1"; String username=Test.getusernamebyid (ID); System.out.println ("*************** username =" +username); } }

A simple example of spring's JDBC (non-web program)

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.