postgresql数据库有默认数据库用户postgres,密码安装库时自己输入;
当然也可以连接其他用户;
maven依赖:
db2依赖
1 2 3 4 5 | <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-dbcp2</artifactId> <scope>provided</scope> </dependency> |
连接postgresql的依赖
1 2 3 4 5 | <dependency> <groupId>postgresql</groupId> <artifactId>postgresql</artifactId> <version>8.2-504.jdbc3</version> </dependency> |
Oracle
1 2 3 4 5 | <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc14</artifactId> <version>10.2.0.2.0</version> </dependency> |
MySQL
1 2 3 4 5 | <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.0.5</version> </dependency> |
QL Server
1 2 3 4 5 | <dependency> <groupId>net.sourceforge.jtds</groupId> <artifactId>jtds</artifactId> <version>1.2</version> </dependency> |
java代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | 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; } } |