日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 连接 PostgreSQL

發布時間:2023/12/19 javascript 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 连接 PostgreSQL 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這個連接比剛才的那個稍微復雜了一點。

我新建了一個Java工程,

工程里面引用了Spring的包,

\spring-framework-3.0.6.RELEASE-with-docs\spring-framework-3.0.6.RELEASE\dist

目錄下面的所有包,可能有些是不需要的,我沒有耐心挑選了

由于使用數據源c3p0,所以引用了

\spring-framework-3.0.1.RELEASE-dependencies\com.mchange.c3p0\com.springsource.com.mchange.v2.c3p0\0.9.1.2\com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar

還引用了

\spring-framework-3.0.1.RELEASE-dependencies\org.apache.commons\com.springsource.org.apache.commons.logging\1.1.1\com.springsource.org.apache.commons.logging-1.1.1.jar

也不知道干嘛用的,不過不引用,好像會報錯,沒有仔細研究了。

當然了還要引用jdbc驅動

postgresql-9.1-901.jdbc4.jar

?

下面編寫Java代碼

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 BeanTest {
public static void main(String[] args)
throws Exception
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
DataSource ds = ctx.getBean("dataSource",DataSource.class);
Connection conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rt = st.executeQuery("select * from weather");
while(rt.next())
{
String test1=rt.getString(1);
System.out.println(test1);
}
rt.close();
st.close();
conn.close();
}

}

?

噢,對了,還忘了一個最重要的,spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
="http://www.springframework.org/schema/beans
classpath:/org/springframework/beans/factory/xml/spring-beans-2.0.xsd"

default-lazy-init
="true">

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method
="close">
<property name="driverClass" value="org.postgresql.Driver"/>
<property name="jdbcUrl" value="jdbc:postgresql:testdb"/>
<property name="user" value="postgres"/>
<property name="password" value="nirvana7"/>
</bean>
</beans>

還有一個要注意的就是spring配置文件放置的位置,這個是跟java文件里面,引用配置文件的方法有關系的,

這里是用的是ClassPathXmlApplicationContext類,所以配置文件要放在這個工程bin目錄下。

OK,可以運行了。



轉載于:https://www.cnblogs.com/nirvana7/archive/2011/12/09/2282504.html

總結

以上是生活随笔為你收集整理的Spring 连接 PostgreSQL的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。