postgresql数据库有默认数据库用户postgres,密码安装库时自己输入;
当然也可以连接其他用户;
maven依赖:

db2依赖

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-dbcp2</artifactId>
    <scope>provided</scope>
</dependency>



连接postgresql的依赖

<dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.2-504.jdbc3</version>
</dependency>

Oracle

<dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.2.0</version>
</dependency>


MySQL

 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.0.5</version>
</dependency>

QL Server

<dependency>
        <groupId>net.sourceforge.jtds</groupId>
        <artifactId>jtds</artifactId>
        <version>1.2</version>
</dependency>

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;


    }


}

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐