这一篇来链接数据库PostgreSQL
1、在pom.xml添加几个依赖
? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ? ? ?
? ? ? ?
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里添加数据源
? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ? ? ?
? ?
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/
------------------------------------------分割线------------------------------------------
下面的文章您可能也喜欢
------------------------------------------分割线------------------------------------------