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