spring mvc 連結 postgresql

來源:互聯網
上載者:User

標籤:

上一篇文章已經分享了搭建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.Driverticket.database.url = jdbc:postgresql://***.dev.cn6.qunar.com:5433/check_resultticket.database.username = menpiao_devticket.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.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一月 27, 2015 11:46:43 下午 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 連結 postgresql

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.