Spring MVC link PostgreSQL

Source: Internet
Author: User

Spring MVC link PostgreSQL

The previous article has shared the following steps to build Spring MVC:

This article links to the 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 the jdbc. properties configuration file.

Ticket. database. driver = org. postgresql. Driver
Ticket. database. url = jdbc: postgresql: // *** .dev.cn6.qunar.com: 5433/check_result
Ticket. database. username = menpiao_dev
Ticket. database. password = ***-***-***

3. Add a data source in the dispatcher-servlet.xml

<Bean id = "dataSource" class = "org. apache. tomcat. jdbc. pool. DataSource"
Destroy-method = "close" autowire = "no">
<Property name = "fairQueue" value = "false"/>
<Property name = "minIdle" value = "1"/>
<Property name = "maxIdle" value = "5"/>
<Property name = "maxActive" value = "5"/>
<Property name = "initialSize" value = "1"/>
<Property name = "testOnBorrow" value = "true"/>
<Property name = "validationQuery" value = "select 1"/>
<Property name = "validationInterval" value = "500000" type = "codeph" text = "codeph"/> <! -- 5 min -->
<Property name = "removeAbandoned" value = "true"/>
<Property name = "removeAbandonedTimeout" value = "30"/>
<Property name = "driverClassName" value = "$ {ticket. database. driver}"/>
<Property name = "url" value = "$ {ticket. database. url}"/>
<Property name = "username" value = "$ {ticket. database. username}"/>
<Property name = "password" value = "$ {ticket. database. password}"/>
</Bean>

4. Create a Test Service Class

Package com. qunar. check. Service;

Import java. SQL. Connection;
Import java. SQL. ResultSet;
Import java. SQL. Statement;
Import javax. SQL. DataSource;
Import org. springframework. context. ApplicationContext;
Import org. springframework. context. support. ClassPathXmlApplicationContext;

Public class TestService {
Public void test (){
Try {
ApplicationContext ctx = new ClassPathXmlApplicationContext ("dispatcher-servlet.xml ");
DataSource ds = ctx. getBean ("dataSource", DataSource. class );
Connection conn = ds. getConnection ();
Statement st = conn. createStatement ();
ResultSet rt = st.exe cuteQuery ("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 void main (String args []) {
TestService t = new TestService ();
T. test ();
}
}

5. test:

INFO: Pre-instantiating singletons in org. springframework. beans. factory. support. defaultListableBeanFactory @ 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
January 27 11:46:43 org. springframework. web. servlet. handler. AbstractUrlHandlerMapping registerHandler
INFO: Mapped URL path [/index. do] onto handler [com. qunar. check. Controller. TestController @ 75be5b6]
Test

:

------------------------------------------ Split line ------------------------------------------

Free in http://linux.bkjia.com/

The username and password are both www.bkjia.com

The specific download directory is in/July 6/July 8/July 8/Spring MVC link PostgreSQL/

For the download method, see

------------------------------------------ Split line ------------------------------------------

You may also like the following articles

Install PostgreSQL 6.3 on yum in CentOS 9.3

PostgreSQL cache details

Compiling PostgreSQL on Windows

Configuration and installation of LAPP (Linux + Apache + PostgreSQL + PHP) Environment in Ubuntu

Install and configure phppgAdmin on Ubuntu

Install PostgreSQL9.3 on CentOS

Configure a Streaming Replication cluster in PostgreSQL

How to install PostgreSQL 7/6 and phpPgAdmin in CentOS 5/6. 4

------------------------------------------ Split line ------------------------------------------

PostgreSQL details: click here
PostgreSQL: click here

This article permanently updates the link address:

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.