IDEA连接postgressql数据库操作

所属分类: 软件编程 / java 阅读数: 87
收藏 0 赞 0 分享

打开IDEA后选择Database数据库选项卡

点击加号标志,选择Data Source,在弹出选项中选择PostgreSQL数据库

填入配置信息,点击Test Connection按钮测试是否连接成功,然后点击ok

补充知识:IDEA spring boot 连接Postgresql配置 【已解决】

1.IDEA创建项目

修改 C:\Program Files\PostgreSQL\9.4\data路径下的 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;

/**
 * @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

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数据库操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

利用MultipartFile实现文件上传功能

这篇文章主要为大家详细介绍了利用MultipartFile实现文件上传功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java编程实现NBA赛事接口调用实例代码

这篇文章主要介绍了Java编程实现NBA赛事接口调用实例代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java编程之双重循环打印图形

这篇文章主要介绍了Java编程之双重循环打印图形,属于Java编程基础练习部分,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

java基础学习JVM中GC的算法

这篇文章主要介绍了java基础学习JVM中GC的算法,通过图文加深对GC算法思路的理解。
收藏 0 赞 0 分享

Java编程Post数据请求和接收代码详解

这篇文章主要介绍了Java编程Post数据请求和接收代码详解,涉及enctype的三种编码,post与get等相关内容,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Retrofit+Rxjava实现文件上传和下载功能

这篇文章主要介绍了Retrofit+Rxjava实现文件上传和下载功能,文中提到了单文件上传和多文件上传及相关参数的请求,需要的朋友参考下吧
收藏 0 赞 0 分享

Retrofit+Rxjava下载文件进度的实现

这篇文章主要介绍了Retrofit+Rxjava下载文件进度的实现,非常不错,具有参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java检查服务器的连通两种方法代码分享

这篇文章主要介绍了java检查服务器的连通两种方法代码分享,涉及ping的介绍以及检查服务器连通的两种方法代码示例,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享

Java/Android 获取网络重定向文件的真实URL的示例代码

本篇文章主要介绍了Java/Android 获取网络重定向文件的真实URL的示例代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java并发编程之同步器代码示例

这篇文章主要介绍了java并发编程之同步器代码示例,分享了相关代码,具有一定参考价值,需要的朋友可以了解下。
收藏 0 赞 0 分享
查看更多