標籤:
上一篇文章已經分享了搭建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