Spring MVC 連結 PostgreSQL

來源:互聯網
上載者:User

Spring MVC 連結 PostgreSQL

前面一篇文章已經分享了搭建Spring MVC:

這一篇來連結資料庫PostgreSQL

1、在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、建立jdbc.properties設定檔

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、在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" /><!-- 5min -->
        <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、建立測試Service類

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.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 void main(String args[]){
        TestService t = new TestService();
        t.test();
    }
}

5、測試:

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
一月 27, 2015 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

------------------------------------------分割線------------------------------------------

免費在 http://linux.bkjia.com/

使用者名稱與密碼都是www.bkjia.com

具體下載目錄在 /2015年資料/2月/8日/Spring MVC 連結 PostgreSQL/

下載方法見

------------------------------------------分割線------------------------------------------

下面的文章您可能也喜歡

CentOS 6.3環境下yum安裝PostgreSQL 9.3

PostgreSQL緩衝詳述

Windows平台編譯 PostgreSQL

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)環境的配置與安裝

Ubuntu上的phppgAdmin安裝及配置

CentOS平台下安裝PostgreSQL9.3

PostgreSQL配置Streaming Replication叢集

如何在CentOS 7/6.5/6.4 下安裝PostgreSQL 9.3 與 phpPgAdmin 

------------------------------------------分割線------------------------------------------

PostgreSQL 的詳細介紹:請點這裡
PostgreSQL 的:請點這裡

本文永久更新連結地址:

相關文章

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.