Spring MVC link PostgreSQL

Source: Internet
Author: User
Tags postgresql ticket

Previous article has shared the building springmvc:http://www.cnblogs.com/liqiu/p/4252788.html

This article to link database PostgreSQL

1. Add several dependencies in Pom.xml

        <Dependency>            <groupId>Org.postgresql</groupId>            <Artifactid>PostgreSQL</Artifactid>            <version>9.3-1102-jdbc4</version>        </Dependency>        <Dependency>            <groupId>Org.apache.tomcat</groupId>            <Artifactid>Tomcat-jdbc</Artifactid>            <version>8.0.9</version>        </Dependency>

2. Create a jdbc.properties configuration file

Ticket.database.driver =  Org.postgresql.Driverticket.database.url = jdbc:postgresql://***.dev.cn6.qunar.com : 5433/check_resultticket.database.username = Menpiao_devticket.database.password = ***-***-***

3. Adding data sources in Dispatcher-servlet.xml

    <BeanID= "DataSource"class= "Org.apache.tomcat.jdbc.pool.DataSource"Destroy-method= "Close"Autowire= "No">        < Propertyname= "Fairqueue"value= "false" />        < Propertyname= "Minidle"value= "1" />        < Propertyname= "Maxidle"value= "5" />        < Propertyname= "Maxactive"value= "5" />        < Propertyname= "InitialSize"value= "1" />        < Propertyname= "Testonborrow"value= "true" />        < Propertyname= "Validationquery"value= "Select 1" />        < Propertyname= "Validationinterval"value= "500000" /><!--5min -        < Propertyname= "removeabandoned"value= "true" />        < Propertyname= "Removeabandonedtimeout"value= "+" />        < Propertyname= "Driverclassname"value= "${ticket.database.driver}" />        < Propertyname= "url"value= "${ticket.database.url}" />        < Propertyname= "username"value= "${ticket.database.username}" />        < Propertyname= "Password"value= "${ticket.database.password}" />    </Bean>

4. Create a Test service class

 PackageCom.qunar.check.Service;Importjava.sql.Connection;ImportJava.sql.ResultSet;Importjava.sql.Statement;ImportJavax.sql.DataSource;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; Public classTestservice { Public voidTest () {Try{ApplicationContext CTX=NewClasspathxmlapplicationcontext ("Dispatcher-servlet.xml"); DataSource DS= Ctx.getbean ("DataSource", DataSource.class); Connection Conn=ds.getconnection (); Statement St=conn.createstatement (); ResultSet RT= St.executequery ("Select * from DataSource");  while(Rt.next ()) {String test1= Rt.getstring (2);            System.out.println (test1);            } rt.close ();            St.close ();        Conn.close (); } Catch(Exception e) {System.out.println (e); } finally {        }    }         Public Static voidMain (String args[]) {Testservice T=NewTestservice ();    T.test (); }}

5. Test:

Info:pre-instantiating singletons in org.s[email protected]ff7f824:defining beans [ Org.springframework.web.servlet.mvc.annotation.defaultannotationhandlermapping#0, Org.springframework.format.support.formattingconversionservicefactorybean#0, Org.springframework.web.servlet.mvc.annotation.annotationmethodhandleradapter#0, Org.springframework.web.servlet.handler.mappedinterceptor#0, Org.springframework.beans.factory.config.propertyplaceholderconfigurer#0,testcontroller, Org.springframework.context.annotation.internalConfigurationAnnotationProcessor, Org.springframework.context.annotation.internalAutowiredAnnotationProcessor, Org.springframework.context.annotation.internalRequiredAnnotationProcessor, Org.springframework.context.annotation.internalCommonAnnotationProcessor, Org.springframework.web.servlet.view.internalresourceviewresolver#0, DataSource]; Root of factory hierarchy January27, 2015 11:46:43pm org.springframework.web.servlet.handler.AbstractUrlHandlerMapping registerhandlerinfo:mapped URL Path [/index. Do] onto handler [[email protected]]test 

: http://files.cnblogs.com/files/liqiu/check_result_db.tar.gz

Spring MVC link PostgreSQL

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.