IDEA連接postgressql數(shù)據(jù)庫(kù)操作
打開(kāi)IDEA后選擇Database數(shù)據(jù)庫(kù)選項(xiàng)卡
點(diǎn)擊加號(hào)標(biāo)志,選擇Data Source,在彈出選項(xiàng)中選擇PostgreSQL數(shù)據(jù)庫(kù)
填入配置信息,點(diǎn)擊Test Connection按鈕測(cè)試是否連接成功,然后點(diǎn)擊ok
補(bǔ)充知識(shí):IDEA spring boot 連接Postgresql配置 【已解決】
1.IDEA創(chuàng)建項(xiàng)目
修改 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對(duì)應(yīng)的 METHOD修改成trust就可以使用。我的電腦采用的ipv4,所以我修改的是ipv4的METHOD為trust。
2.創(chuàng)建application.yml文件,寫(xiě)入驅(qū)動(dòng)接口
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)@SpringBootTestpublic class JdbcTestApplicationTests { @Autowired DataSource dataSource; @Test public void contextLoads() throws SQLException { System.out.println('連接成功'); System.out.println('dataSource.getClass()內(nèi)容***'+dataSource.getClass()); Connection connection = dataSource.getConnection(); System.out.println('connection內(nèi)容***'+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 */@Controllerpublic class controller { /** * @Autowired 注釋,它可以對(duì)類成員變量、方法及構(gòu)造函數(shù)進(jìn)行標(biāo)注,完成自動(dòng)裝配的工作。 通過(guò) @Autowired的使用來(lái)消除 set ,get方法。 * 在使用@Autowired之前,我們對(duì)一個(gè)bean配置起屬性時(shí),是這用的 */ @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.sqlspring.datasource.initialization-mode=always
運(yùn)行結(jié)果
以上這篇IDEA連接postgressql數(shù)據(jù)庫(kù)操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. WML教程之文本框控件Input2. 詳解CSS偽元素的妙用單標(biāo)簽之美3. XML入門(mén)的常見(jiàn)問(wèn)題(三)4. 利用CSS3新特性創(chuàng)建透明邊框三角5. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例6. 不要在HTML中濫用div7. 多級(jí)聯(lián)動(dòng)下拉選擇框,動(dòng)態(tài)獲取下一級(jí)8. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)9. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程10. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼
