JDBC连接postgresql例子

2023-11-02

package tool;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class PsqlConnectionTool {
    private String url = "jdbc:postgresql://xxx.xxx.xxx.xxx:5432/testdb";
    private String username = "postgres";
    private String password = "postgres";
    private Connection connection = null;

    public Connection getConn() {
        try {
            Class.forName("org.postgresql.Driver").newInstance();
            connection = DriverManager.getConnection(url, username, password);
        } catch (InstantiationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return connection;
    }


    public ResultSet query(Connection conn, String sql) {
        PreparedStatement pStatement = null;
        ResultSet rs = null;
        try {
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        } 
        return rs;
    }

    public boolean queryUpdate(Connection conn, String sql) {
        PreparedStatement pStatement = null;
        int rs = 0;
        try {
            pStatement = conn.prepareStatement(sql);
            rs = pStatement.executeUpdate();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        if (rs > 0) {
            return true;
        }
        return false;
    }



    public static void main(String[] args) throws SQLException {
        PsqlConnectionTool pgtool = new PsqlConnectionTool();
        Connection myconn = pgtool.getConn();
        pgtool.queryUpdate(myconn, "insert into test values (1,'smoon','man')");
        ResultSet rs = pgtool.query(myconn, "select * from test");
        while(rs.next()){                       
            int id = rs.getInt("id");       
            String name = rs.getString("name");
            String gender = rs.getString("gender");
            System.out.println("id:"+id+" 姓名:"+name+" 性别:"+gender);
            myconn.close();
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JDBC连接postgresql例子 的相关文章

随机推荐

  • U-Boot 启动流程详解

    文章目录 链接脚本 u boot lds 详解 Uboot启动流程 链接脚本 u boot lds 详解 要分析 uboot 的启动流程 首先要找到 入口 找到第一行程序在哪里 程序的链接是由链接脚本来决定的 所以通过链接脚本可以找到程序的
  • linux驱动模块编译Makefile

    该Makefile最好是和hello c同样的路径 obj m hello o 表示编译生成的模块 前缀hello必须和 c文件相同 all make C lib modules shell uname r build M PWD modu
  • /bin/sh: 1: nvcc: not found Makefile:89: recipe for target 'obj/convolutional_kernels.o' failed

    在测试YOLO时出现 nvcc gencode arch compute 30 code sm 30 gencode arch compute 35 code sm 35 gencode arch compute 50 code sm 50
  • 高性能内存分配器 jemalloc 基本原理

    Netty 内存管理的实现并不是一蹴而就的 它也是参考了 jemalloc 内存分配器 今天我们就先介绍 jemalloc 内存分配器的基本原理 为我们后面的课程打好基础 背景知识 jemalloc 是由 Jason Evans 在 Fre
  • [Warning] ‘typedef‘ was ignored in this declaration解决

    首先先展示一下问题是怎么样的 出现了 typedef was ignored in this declaration的问题 下面提供解决方案及原因 原因 在于使用结构体的时候没有使用别名 解决方案 1 去掉typedef 2 在结构体后面加
  • elementui tree怎样设置默认勾选

    在树形菜单数据里面设置一个唯一的字段 这里我的就是id字段 然后设置node key为那个唯一字段 然后用default checked keys绑定一个数组 这个数组里面就是需要勾选的菜单所对应的的id 比如我这里的是9 这样运行之后菜单
  • echarts使用结合时间轴timeline动态刷新案例

    1 echarts简介 ECharts 一个使用 JavaScript 实现的开源可视化库 可以流畅的运行在 PC 和移动设备上 兼容当前绝大部分浏览器 IE8 9 10 11 Chrome Firefox Safari等 底层依赖轻量级的
  • unity的UGUI的mask(遮罩)的使用

    之前我写过一篇博客关于UGUI的优化 其中提到了Mask的使用会增加性能的消耗 但是在一些情况下 使用这个会有奇效 比如小地图 Minimap 的开发 这篇博客介绍一下UGUI中的Mask的使用方法 很简单的 首先创建一个 Image 给他
  • 【PHP发送邮件】PHP实现发送邮件

    PHP发送邮件 Thinkphp直接使用 其他框架修改使用 1 安装 composer require phpmailer phpmailer 2 填写配置表 配置文件mail php
  • MQTT协议介绍

    1 MQTT协议简介 MQTT Message Queuing Telemetry Transport 消息队列遥测传输 是一个轻量的发布 订阅模式消息传输协议 是专门针对低带宽和不稳定网络环境的物联网应用设计的 特点 1 开放消息协议 易
  • Spring Data HelloWorld(三)

    在 Spring Data 环境搭建 二 的基础之上 我们改动 定义个一个接口 继承Repository类 咱们先实现一个根据名字查询 package org springdata repository import org springd
  • Python开发篇——添加mysqlclient

    最近使用mysql8 0 于是我就尝试用Django的框架 但是执行poetry add mysqlclient却出现了错误 python3 7 dison dison X450LD workstation project script s
  • Kuberneters企业级容器云平台落地实践之二

    九 日志中心 1 filebeat安装 Filebeat 是一个用于转发和集中日志数据的轻量级传送器 作为代理安装在您的服务器上 Filebeat 监控您指定的日志文件或位置 收集日志事件 并将它们转发到Elasticsearch或 Log
  • Linux下装载Qt

    Linux下装载Qt 官网文件下载Qt 本官网地址 http download qt io archive qt 5 9 5 9 6 https www qt io offline installers 将文件放置Linux目录下 将随意一
  • 6.SpringBoot Web开发-webjars&静态资源映射规则(欢迎页和角标favicon.ico替换)

    文章总结 作为一个后端开发 在Springboot中怎样引入需要的js依赖以及常用的静态资源映射呢 SpringBoot已经给做好了自动化配置 使用时只需要按照默认的配置去放相应的文件 就可以快速上手 1 创建SpringBoot web项
  • ArrayList,List 的区别;

    List 的区别 6 数组 数组 内存中是连续存储的 索引速度非常快 赋值与修改元素也很简单 但不 利于动态扩展以及移动 ArrayList 因为数组的缺点 就产生了 ArrayList ArrayList 使用该类时必须进行引用 同时继承
  • 给语音信号加混响的常用方法(方法一)

    使用python包 pyroomacoustic 给干净语音加混响 Pyroomacoustics是一款旨在快速开发和测试音频阵列处理算法的软件包 包的内容可分为三个主要组成部分 1 直观的Python面向对象接口 可快速构建2D和3D房间
  • 2020年10月蓝桥杯原题寻找2020

    这题比较简单 主要是以行 列 斜线的方式寻找2020的个数 注意的一点就是控制下标越界的情况 答案是16520 public class test4 public static String readTxt File file String
  • 计算机英文专业文献翻译,计算机专业英文文献翻译.doc

    文档介绍 计算机英文文献翻译 计算机英文文献翻译 INDUSTTRY PERSPECTIVE USING A DSS TO KEEP THE COST OF GAS DOWN Think you spend a lot on gas for
  • JDBC连接postgresql例子

    package tool import java sql Connection import java sql DriverManager import java sql PreparedStatement import java sql