JAVA-JDBC Database Connection

Source: Internet
Author: User
Tags aop

Web. xml: (Web. xml)

<!--loading the spring container -    <Context-param>        <Param-name>Contextconfiglocation</Param-name>        <Param-value>Classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-shiro.xml</Param-value>    </Context-param>

Mybatis.xml: (Spring-mybatis.xml)

<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd        Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.3.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.3.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.3.xsd ">    <!--Import Resource File -    <!--load the contents of the Db.properties file, the key name in the Db.properties file must have some special rules -    <Context:property-placeholder Location= "Classpath:jdbc.properties" />     <!--configuring C3P0 Data Sources -    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">        <!--connection parameters for the database -        < Propertyname= "User"value= "${jdbc.user}"></ Property>        < Propertyname= "Password"value= "${jdbc.password}"></ Property>        < Propertyname= "Driverclass"value= "${jdbc.driverclass}"></ Property>        < Propertyname= "Jdbcurl"value= "${jdbc.jdbcurl}"></ Property>        < Propertyname= "Initialpoolsize"value= "${jdbc.initalpoolsize}"></ Property>        < Propertyname= "Maxpoolsize"value= "${jdbc.maxpoolsize}"></ Property>    </Bean>         <!--Configure Namedparameterjdbctemplate, which can make the appliance name parameter, which has no parameterless constructor, must specify parameters for its constructor -     <BeanID= "Namedparameterjdbctemplate"class= "Org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">         <Constructor-argref= "DataSource"></Constructor-arg>     </Bean></Beans>

Userdao.java

@Repository Public classUserdao { PublicUser finduserbyloginname (String loginName) {String sql= "SELECT * from user where LoginName =: LoginName"; User User=NewUser ();        User.setloginname (LoginName); //User.setpassword (PassWord);Sqlparametersource Parametersource =Newbeanpropertysqlparametersource (user); Beanpropertyrowmapper<User> RowMapper =NewBeanpropertyrowmapper<user> (User.class); NewJdbcmysql (); Namedparameterjdbctemplate namedparameterjdbctemplate=jdbcmysql.namedparameterjdbctemplate; Try{User=Namedparameterjdbctemplate.queryforobject (Sql,parametersource,rowmapper); } Catch(DataAccessException e) {return NULL; }        returnuser; }}
Jdbcmysql.java
 Public classJdbcmysql {Private StaticApplicationContext CTX =NULL;  Public Staticnamedparameterjdbctemplate namedparameterjdbctemplate;    {        Try {            if(CTX = =NULL) {CTX=NewClasspathxmlapplicationcontext ("Spring-mybatis.xml"); } namedparameterjdbctemplate= (namedparameterjdbctemplate) Ctx.getbean (namedparameterjdbctemplate.class); /*combopooleddatasource pool= (combopooleddatasource) Ctx.getbean ("DataSource");            Jdbcuser = Pool.getuser ();            Jdbcpassword = Pool.getpassword ();            Jdbcurl = Pool.getjdbcurl ();            Driverclass = Pool.getdriverclass ();            Initialpoolsize = Pool.getinitialpoolsize (); Maxpoolsize = Pool.getmaxpoolsize ();*/        } Catch(Exception e) {System.out.println ("Error:" +e.getmessage () +e.getstacktrace ()); }    }}

JAVA-JDBC Database Connection

Related Article

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.