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
        destroy-method="close" autowire="no">
        <property value="false" />
        <property value="1" />
        <property value="5" />
        <property value="5" />
        <property value="1" />
        <property value="true" />
        <property value="select 1" />
        <property value="500000" /><!-- 5min -->
        <property value="true" />
        <property value="30" />
        <property value="${ticket.database.driver}" />
        <property value="${ticket.database.url}" />
        <property value="${ticket.database.username}" />
        <property 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

下载地址:

------------------------------------------分割线------------------------------------------

免费下载地址在

用户名与密码都是

具体下载目录在 /2015年资料/2月/8日/Spring MVC 链接 PostgreSQL/

下载方法见

------------------------------------------分割线------------------------------------------

下面的文章您可能也喜欢

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/7735bcd9b350f4e0d1bd43a785f3d94a.html