当前位置: 移动技术网 > IT编程>开发语言>Java > IDEA连接postgressql数据库操作

IDEA连接postgressql数据库操作

2020年08月29日  | 移动技术网IT编程  | 我要评论
打开idea后选择database数据库选项卡点击加号标志,选择data source,在弹出选项中选择postgresql数据库填入配置信息,点击test connection按钮测试是否连接成功,

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

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网