postgresql数据库有默认数据库用户postgres,密码安装库时自己输入;
当然也可以连接其他用户;
maven依赖:
db2依赖
org.apache.commons
commons-dbcp2
provided
连接postgresql的依赖
postgresql
postgresql
8.2-504.jdbc3
Oracle
com.oracle
ojdbc14
10.2.0.2.0
MySQL
mysql
mysql-connector-java
5.0.5
QL Server
net.sourceforge.jtds
jtds
1.2
java代码:
package com.weimanage.data;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
import java.util.Properties;
public class getDataSource {
@Bean(name="dataSource")
public static DataSource getDataSource(){
Properties props = new Properties();
props.setProperty("driver","org.postgresql.Driver");
props.setProperty("url","jdbc:postgresql://127.0.0.1:5432/postgres");
props.setProperty("user","postgres");
props.setProperty("password ","1");
DataSource dataSource = null;
try {
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
e.printStackTrace();
}
return dataSource;
}
}