Based on debian 9, postgresql 9.6 and Java 8, at Dec-24-2018

=================================================
1. Debain Install
=================================================

* Install DB and JDBC

sudo aptitude install postgresql libpostgresql-jdbc-java
 
        --below will be installed
        default-jdk-doc{a} libpostgresql-jdbc-java libpostgresql-jdbc-java-doc openjdk-8-doc{a} postgresql-9.6
        
* Configure

1. re-configure listner

sudo vim  /etc/postgresql/9.6/main/postgresql.conf
        listen_addresses = '*'        # what IP address(es) to listen on;

2. stop / start / status
        
        The service: /lib/systemd/system/postgresql.service
        sudo systemctl stop/start postgresql.service

* Reference

[PostgreSQL Java tutorial](http://www.postgresqltutorial.com/postgresql-jdbc/)

=================================================
2. create user / db /table /data
=================================================

* create db user / database / table and grant privi

sudo passwd postgresql
    create user dbuser with password 'dbuser';
    create database exampledb with owner dbuser;
    grant all privileges on database exampledb to dbuser;

* Login and manage data

psql -U dbuser -d exampledb -h 127.0.0.1 -p 5432

CREATE TABLE emp(name VARCHAR(20), id integer, signup_date DATE);
    插入数据

INSERT INTO emp(name, id, signup_date) VALUES('cliff',1,'2011-12-22');
    INSERT INTO emp(name, id, signup_date) VALUES('jim',2,'2012-12-22');
    INSERT INTO emp(name, id, signup_date) VALUES('andy',3,'2013-12-22');
    INSERT INTO emp(name, id, signup_date) VALUES('wind',4,'2014-12-22');

alter table emp add column resume bytea;
    
=================================================
2. A Query to get column name automatically AND multiple parameters in IN cluase
=================================================

* Demo connection build / close
* Demo SQL query - ResultSetMetaData usage and pass multiple values to IN (?)
* Demo byteA/blob update
* Demo byteA/blob read

        import java.io.*;
import java.sql.*;
public class FirstDemo { private Connection conn=null; /*
* get connection
*/
public void getConn(){ String url="jdbc:postgresql://localhost/exampledb", user="dbuser", password="dbuser"; try {
conn=DriverManager.getConnection(url, user, password);
System.out.println("The connection build as "+conn.getSchema());
} catch (SQLException e) {
System.out.println(e.getErrorCode() + e.getSQLState() + e.getMessage());
e.printStackTrace();
}
} /*
* get connection
*/
public void CloseConn(){
try {
conn.close();
} catch (SQLException e) {
System.out.println(e.getErrorCode() + e.getSQLState() + e.getMessage());
e.printStackTrace();
}
} /*
* Run a query and return the result
* 1. get the column name from ResultSetMetaData
* 2. use "nest" to pass multiple parameters to in ( ? )
*/
public void query(){
System.out.println("Query starting :");
Integer[] ids={1,2,3};
try {
PreparedStatement pst=conn.prepareStatement("select * from emp where id in (SELECT * FROM unnest(?))");
//pst.setInt(1, 1);
//pst.setInt(2, 2);
//pst.setInt(3, 3);
Array a = conn.createArrayOf("integer", ids);
pst.setArray(1, a);
ResultSet rs=pst.executeQuery();
ResultSetMetaData rsmd=rs.getMetaData();
System.out.printf("%-20s%-20s\n", rsmd.getColumnName(1), rsmd.getColumnName(2)); while(rs.next()) { System.out.printf("%-20s%-20d\n", rs.getString(1), rs.getInt(2)); }
pst.close(); } catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{ } } /*
* Run a query and return the result
* byteA / blob read
*/
public void byteARead(){
System.out.println("byteA reading :");
File f;
FileOutputStream fos;
byte[] b = null;
PreparedStatement pst;
try { f = new File("/home/ftian/postgresqlCliffReadme.txt");
fos = new FileOutputStream (f); pst=conn.prepareStatement("select resume from emp where id =1");
ResultSet rs=pst.executeQuery();
while(rs.next()) { b = rs.getBytes(1); }
fos.write(b);
fos.close();
pst.close();
System.out.println("byteA reading done"); } catch (Exception e) {
e.printStackTrace();
}
} /*
* Run a query and return the result
* byteA / blob insert
*/
public void byteAInsert(){
System.out.println("byteA inserting :");
File f;
FileInputStream fis;
PreparedStatement pst;
try { f = new File("/home/ftian/workspace/postgres/postgresqlCliffReadme.txt");
fis = new FileInputStream (f); pst=conn.prepareStatement("update emp set resume=? where id =?");
pst.setBinaryStream(1, fis, (int)f.length());
pst.setInt(2, 1);
pst.executeUpdate();
System.out.println("byteA inserting done "+pst.getUpdateCount());
fis.close();
pst.close(); } catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args) { FirstDemo fd = new FirstDemo();
fd.getConn();
fd.query();
fd.byteAInsert();
fd.byteARead();
fd.CloseConn(); } }

postgresql + JDBC 学习的更多相关文章

  1. JDBC学习笔记(2)——Statement和ResultSet

    Statement执行更新操作 Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数据库连接的基础上,向数据库发送要执行的SQL语句.Statement ...

  2. JDBC学习笔记(1)——JDBC概述

    JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据库连接. JDBC库中所包含的API任务通常与数据库使用: 连接到数 ...

  3. 【转】JDBC学习笔记(2)——Statement和ResultSet

    转自:http://www.cnblogs.com/ysw-go/ Statement执行更新操作 Statement:Statement 是 Java 执行数据库操作的一个重要方法,用于在已经建立数 ...

  4. 【转】JDBC学习笔记(1)——JDBC概述

    转自:http://www.cnblogs.com/ysw-go/ JDBC JDBC API是一个Java API,可以访问任何类型表列数据,特别是存储在关系数据库中的数据.JDBC代表Java数据 ...

  5. jdbc学习总结

    jdbc学习总结:   一.简介: jdbc,直译为java连接数据库.实际为java为很好的操作数据库而提供的一套接口,接口的实现(即驱动)由各个数据库厂商提供.   二.知识要点: 连接5要素,3 ...

  6. JDBC 学习笔记(十一)—— JDBC 的事务支持

    1. 事务 在关系型数据库中,有一个很重要的概念,叫做事务(Transaction).它具有 ACID 四个特性: A(Atomicity):原子性,一个事务是一个不可分割的工作单位,事务中包括的诸操 ...

  7. JDBC 学习笔记(十)—— 使用 JDBC 搭建一个简易的 ORM 框架

    1. 数据映射 当我们获取到 ResultSet 之后,显然这个不是我们想要的数据结构. 数据库中的每一个表,在 Java 代码中,一定会有一个类与之对应,例如: package com.gerrar ...

  8. JDBC 学习笔记(六)—— PreparedStatement

    1. 引入 PreparedStatement PreparedStatement 通过 Connection.createPreparedStatement(String sql) 方法创建,主要用 ...

  9. JDBC学习笔记二

    JDBC学习笔记二 4.execute()方法执行SQL语句 execute几乎可以执行任何SQL语句,当execute执行过SQL语句之后会返回一个布尔类型的值,代表是否返回了ResultSet对象 ...

随机推荐

  1. Graphics-Processing Architecture Based on Approximate Rendering

    BACKGROUND The present invention generally relates to the processing of graphics data, and particula ...

  2. Spring Boot 学习笔记一(SpringBoot启动过程)

    SpringBoot启动 Spring Boot通常有一个名为*Application的入口类,在入口类里有一个main方法,这个main方法其实就是一个标准的java应用的入口方法. 在main方法 ...

  3. iOS 使用贝塞尔曲线绘制路径

    使用贝塞尔曲线绘制路径 大多数时候,我们在开发中使用的控件的边框是矩形,或者做一点圆角,是使得矩形的角看起来更加的圆滑. 但是如果我们想要一个不规则的图形怎么办?有人说,叫UI妹子做,不仅省事,还可以 ...

  4. 动态备份SQL-SERVER数据库——SQLDMO

    转载:http://www.cnblogs.com/liulanglang/archive/2007/12/04/981812.html 上周要写一个SQL-SERVER数据库备份还原的程序,很没有思 ...

  5. 【37.00%】【vijos p1425】子串清除

    P1425子串清除Accepted 标签:[显示标签] 描述 我们定义字符串A是字符串B的子串当且仅当我们能在B串中找到A串.现在给你一个字符串A,和另外一个字符串B,要你每次从B串中从左至右找第一个 ...

  6. RGB值得计算公式

    三原色分别为:红(Red).绿(Green).蓝(Blue). 颜色值=(Red)+(Green*256)+(Blue*256*256) //由三原色值合成颜色整数值 function ColorFr ...

  7. create-react-app 支持 装饰器 decorator

    网上常见方法全是安装 babel-plugin-transform-decorators-legacy 然后添加babel配置的, 实际情况是最新版本的create-react-app 生成的项目已经 ...

  8. 回归(regression)的理解(regressor,回归子)

    1. 基本概念 回归(regression)是监督学习(given {(xi,yi)})的一个重要分类.回归用于预测输入变量(自变量,Xi)与输出变量(因变量,Yi) 之间的关系,特定是当输入变量的值 ...

  9. mysql和mysqli使用笔记

    总体来说还算兼容得还行,很多函数直接加个i 即可,比如mysql_connect -> mysqli_connect. 有些细小的区别,mysqli 更严格,当变量有与字段重名时,改变变量名才可 ...

  10. Coverage数据拓扑

    什么是Coverage?   Coverage数据模型源于ESRI公司1981年推出的第一个商业GIS软件——ArcInfo.也被称为地理相关数据模型(Georelational Data Model ...