打开IDEA后选择Database数据库选项卡
点击加号标志,选择Data Source,在弹出选项中选择PostgreSQL数据库
填入配置信息,点击Test Connection按钮测试是否连接成功,然后点击ok
补充知识:IDEA spring boot 连接Postgresql配置 【已解决】
1.IDEA创建项目
修改 C:Program FilesPostgreSQL9.4data路径下的 pg_hba.conf配置信息
# METHOD can be "trust", "reject", "md5", "password", "gss", "sspi", # "ident", "peer", "pam", "ldap", "radius" or "cert". Note that # "password" sends passwords in clear text; "md5" is preferred since # it sends encrypted passwords.
这里解释了配置信息,我们只需要将自己电脑ipv4/ipv6对应的 METHOD修改成trust就可以使用。我的电脑采用的ipv4,所以我修改的是ipv4的METHOD为trust。
2.创建application.yml文件,写入驱动接口
spring: datasource: url: jdbc:postgresql://172.30.105.178:5432/mysql?useSSL=false username: postgres password: 0000 driverClassName: org.postgresql.Driver
JpaPostgresqlApplicationTests.java
package com.qingsong.jdbc_test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
@RunWith(SpringRunner.class)
@SpringBootTest
public class JdbcTestApplicationTests {
@Autowired
DataSource dataSource;
@Test
public void contextLoads() throws SQLException {
System.out.println("连接成功");
System.out.println("dataSource.getClass()内容***"+dataSource.getClass());
Connection connection = dataSource.getConnection();
System.out.println("connection内容***"+connection);
connection.close();
}
}
controller.java
package com.qingsong.mybatis_mysql.control;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
import java.util.Map;
@Controller
public class controller {
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/hi")
public Map map(){
List
Author.sql
create table Author ( code varchar(20) primary key, name varchar(20) not null );
application.properties
# schema.sql中一般存放的是DDL脚本 spring.datasource.schema=classpath:Author.sql spring.datasource.initialization-mode=always
运行结果
以上这篇IDEA连接postgressql数据库操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。



