打开IDEA后选择Database数据库选项卡
点击加号标志,选择Data Source,在弹出选项中选择PostgreSQL数据库
填入配置信息,点击Test Connection按钮测试是否连接成功,然后点击ok
补充知识:IDEA spring boot 连接Postgresql配置 【已解决】
1.IDEA创建项目
修改 C:\\Program Files\\PostgreSQL\\9.4\\data路径下的 pg_hba.conf配置信息
?
|
1
2
3
4
|
# 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文件,写入驱动接口
?
|
1
2
3
4
5
6
|
spring:
datasource:
url: jdbc:postgresql://172.30.105.178:5432/mysql?useSSL=false
username: postgres
password: 0000
driverClassName: org.postgresql.Driver
|
JpaPostgresqlApplicationTests.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
|
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
?
|
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
31
|
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;
/**
* @Auther: 青松
* @Date: 2019/3/5 20:19
*/
@Controller
public class controller {
/**
* @Autowired 注释,它可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。 通过 @Autowired的使用来消除 set ,get方法。
* 在使用@Autowired之前,我们对一个bean配置起属性时,是这用的
*/
@Autowired
JdbcTemplate jdbcTemplate;
@ResponseBody
@GetMapping("/hi")
public Map<String,Object> map(){
List<Map<String,Object>> list=jdbcTemplate.queryForList("select * from author");
return list.get(0);
}
}
|
Author.sql
?
|
1
2
3
4
5
|
create table Author
(
code varchar(20) primary key,
name varchar(20) not null
);
|
application.properties
?
|
1
2
3
4
|
# schema.sql中一般存放的是DDL脚本
spring.datasource.schema=classpath:Author.sql
spring.datasource.initialization-mode=always
|
运行结果
以上这篇IDEA连接postgressql数据库操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持快网idc。
原文链接:https://blog.csdn.net/weixin_41595700/article/details/88818315
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择适合自己的建站程序或框架? 2025-06-10
- 64M VPS建站:能否支持高流量网站运行? 2025-06-10
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-06-04 81
-
2025-05-25 24
-
2025-05-25 104
-
2025-05-27 34
-
2025-05-25 50
热门评论








