当前位置:Gxlcms > mysql > SpringMVC链接PostgreSQL

SpringMVC链接PostgreSQL

时间:2021-07-01 10:21:17 帮助过:23人阅读

前面一篇文章已经分享了搭建Spring MVC:http://www.linuxidc.com/Linux/2015-02/113103.htm 这一篇来链接数据库PostgreSQL 1、在

前面一篇文章已经分享了搭建Spring MVC:

这一篇来链接数据库PostgreSQL

1、在pom.xml添加几个依赖


org.postgresql
postgresql
9.3-1102-jdbc4


org.apache.tomcat
tomcat-jdbc
8.0.9

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里添加数据源

destroy-method="close" autowire="no">














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 : 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/

下载方法见

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

下面的文章您可能也喜欢

CentOS 6.3环境下yum安装PostgreSQL 9.3

PostgreSQL缓存详述

Windows平台编译 PostgreSQL

Ubuntu下LAPP(Linux+Apache+PostgreSQL+PHP)环境的配置与安装

Ubuntu上的phppgAdmin安装及配置

CentOS平台下安装PostgreSQL9.3

PostgreSQL配置Streaming Replication集群

如何在CentOS 7/6.5/6.4 下安装PostgreSQL 9.3 与 phpPgAdmin

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

PostgreSQL 的详细介绍:请点这里
PostgreSQL 的下载地址:请点这里

本文永久更新链接地址:

人气教程排行