spring mvc 链接 postgresql

上一篇文章已经分享了搭建springmvc:http://www.cnblogs.com/liqiu/p/4252788.html

这一篇来链接数据库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://files.cnblogs.com/files/liqiu/check_result_db.tar.gz

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。