要启动好gp服务,再尝试连接

192.168.94.135是主节点(master)的ip

驱动Jar包在官网获取

嫌麻烦,可以直接用我在网盘分享的Jar包,版本较老

链接:https://pan.baidu.com/s/1gVsxupUNUPXuewYlXHmgyw
提取码:bq1c

package com.advance.JDBC;

import java.sql.*;
import java.util.*; /**
* @Auther: 谷天乐
* @Date: 2018/11/14 16:47
* @Description:
* 通过JDBC连接Greenplum
*/
public class Connect_Greenplum {
//表信息
static class TbInfo
{
//分布键
String id;
//日期
String date;
//价格
String amt; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} public String getAmt() {
return amt;
} public void setAmt(String amt) {
this.amt = amt;
}
} //表结构
static class TbStructure{
//appendonly属性
boolean appendonly = true;
//压缩类型
String compresstype = "zlib";
//压缩级别
int compresslevel = ;
//表的列和类型,用逗号分隔
String columnInfo;
//表名称
String tbName;
//分布键
String distributedKey; public String getCompresstype() {
return compresstype;
} public void setCompresstype(String compresstype) {
this.compresstype = compresstype;
} public boolean isAppendonly() {
return appendonly;
} public void setAppendonly(boolean appendonly) {
this.appendonly = appendonly;
} public int getCompresslevel() {
return compresslevel;
} public void setCompresslevel(int compresslevel) {
this.compresslevel = compresslevel;
} public String getColumnInfo() {
return columnInfo;
} public void setColumnInfo(String columnInfo) {
this.columnInfo = columnInfo;
} public String getTbName() {
return tbName;
} public void setTbName(String tbName) {
this.tbName = tbName;
} public String getDistributedKey() {
return distributedKey;
} public void setDistributedKey(String distributedKey) {
this.distributedKey = distributedKey;
}
} //三大核心接口
private static Connection conn = null;
private static PreparedStatement pstmt = null;
private static ResultSet rs = null; //连接数据库
public static Connection connectGreenplum() throws ClassNotFoundException, SQLException {
// URL
String url = "jdbc:pivotal:greenplum://192.168.94.135:5432;DatabaseName=testdw";
// 数据库用户名
String username = "gpadmin";
// 数据库密码
String password = "gpadmin";
// 加载驱动
Class.forName("com.pivotal.jdbc.GreenplumDriver");
// 获取连接
conn = DriverManager.getConnection(url, username, password);
return conn;
} //关闭数据库连接
public static void closeConnection(){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(pstmt!=null){
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
//查询方法
public static ResultSet query(String sql) throws SQLException, ClassNotFoundException {
Connection conn = connectGreenplum();
pstmt = conn.prepareStatement(sql);
rs = pstmt.executeQuery();
return rs;
} //通用增删改
public static void update(String sql,Object []values) throws SQLException, ClassNotFoundException {
//获取数据库链接
conn=connectGreenplum();
try {
//预编译
pstmt=conn.prepareStatement(sql);
//获取ParameterMetaData()对象
ParameterMetaData pmd=pstmt.getParameterMetaData();
//获取参数个数
int number=pmd.getParameterCount();
//循环设置参数值
for (int i = ; i <=number; i++) {
pstmt.setObject(i, values[i-]);
}
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}finally{
closeConnection();
}
} //新增方法
public static Integer insert(String sql,TbInfo tbInfo) throws SQLException, ClassNotFoundException {
Connection conn = connectGreenplum();
pstmt = conn.prepareStatement(sql);
pstmt.setString(,tbInfo.getId());
pstmt.setString(,tbInfo.getDate());
pstmt.setString(,tbInfo.getAmt());
Integer rs = pstmt.executeUpdate();
return rs;
} //删除方法
public static Integer delete(String sql,TbInfo tbInfo) throws SQLException, ClassNotFoundException {
conn = connectGreenplum();
pstmt = conn.prepareStatement(sql);
pstmt.setString(,tbInfo.getId());
Integer rs = pstmt.executeUpdate();
return rs;
} //修改方法
public static Integer update(String sql,TbInfo tbInfo) throws SQLException, ClassNotFoundException {
conn = connectGreenplum();
pstmt = conn.prepareStatement(sql);
pstmt.setString(,tbInfo.getAmt());
pstmt.setString(,tbInfo.getId());
Integer rs = pstmt.executeUpdate();
return rs;
} //输出
public static void output(ResultSet rs) throws SQLException {
while(rs.next())
{
System.out.println(
"id:"+rs.getString("id")+
" 日期:"+rs.getString("date")+
" 价格:"+rs.getString("amt"));
}
} //ResultSet转换成list
public static List resultSetToList(ResultSet rs) throws java.sql.SQLException {
if (rs == null)
return Collections.EMPTY_LIST;
ResultSetMetaData md = rs.getMetaData(); //得到结果集(rs)的结构信息,比如字段数、字段名等
int columnCount = md.getColumnCount(); //返回此 ResultSet 对象中的列数
List list = new ArrayList();
Map rowData;
while (rs.next()) {
rowData = new HashMap(columnCount);
for (int i = ; i <= columnCount; i++) {
rowData.put(md.getColumnName(i), rs.getObject(i));
}
list.add(rowData);
System.out.println("list:" + list.toString());
}
return list;
} //删除表
public static Integer dropTable(String tableName) throws SQLException, ClassNotFoundException {
conn = connectGreenplum();
String sql = "DROP TABLE if EXISTS "+tableName+";";
pstmt = conn.prepareStatement(sql);
Integer rs = pstmt.executeUpdate();
return rs;
} //删除外部表
public static Integer dropExternalTable(String tableName) throws SQLException, ClassNotFoundException {
conn = connectGreenplum();
String sql = "DROP EXTERNAL TABLE if EXISTS "+tableName+";";
pstmt = conn.prepareStatement(sql);
Integer rs = pstmt.executeUpdate();
return rs;
} //创建表
public static Integer createTable(String tbName,String columnInfo,String distributedKey) throws SQLException, ClassNotFoundException {
conn = connectGreenplum();
TbStructure tbStructure = new TbStructure();
String sql = "CREATE TABLE "+tbName+" ("+columnInfo+")\n" +
"WITH (appendonly="+tbStructure.isAppendonly()+", " +
"compresstype="+tbStructure.getCompresstype()+",\n" +
"compresslevel="+tbStructure.getCompresslevel()+") DISTRIBUTED BY ("+distributedKey+");";
pstmt = conn.prepareStatement(sql);
Integer rs = pstmt.executeUpdate();
return rs;
} //创建可读外部表,需要启动gpfdist服务,再导入csv
public static Integer createExternalTable(String tbName,String columnInfo,String location,
String format,String delimiter) throws SQLException, ClassNotFoundException {
conn = connectGreenplum();
String sql = "CREATE EXTERNAL TABLE "+tbName+" ("+columnInfo+") \n" +
"LOCATION ("+"\'"+location+"\'"+")\n" +
"FORMAT "+"\'"+format+"\'"+" (DELIMITER "+"\'"+delimiter+"\'"+")\n";
pstmt = conn.prepareStatement(sql);
Integer rs = pstmt.executeUpdate();
System.out.println("成功创建外部表");
return rs;
} public static void main(String[] args) {
try {
// 插入功能
/*String insertSql = "insert into tb_cp_02 values(?,?,?);";
TbInfo tbInfo1 = new TbInfo();
tbInfo1.setId("7");
tbInfo1.setDate("2013-06-01");
tbInfo1.setAmt("500.00");
insert(insertSql,tbInfo1);*/
// 删除功能
/*String deleteSql = "delete from tb_cp_02 where id = ?";
TbInfo tbInfo1 = new TbInfo();
tbInfo1.setId("2");
delete(deleteSql,tbInfo1);*/ // 修改功能
/*String updateSql = "update tb_cp_02 set amt = ? where id = ?";
TbInfo tbInfo1 = new TbInfo();
tbInfo1.setId("3");
tbInfo1.setAmt("1001.0");
update(updateSql,tbInfo1);*/ /*for (TbInfo tb:tbInfos
) {
System.out.println(tb.getId());
}*/
//dropTable("tb_tag_1_read");
//createTable("foo","a int, b text","a");
// dropExternalTable("tb_tag_1_read");
// createExternalTable("tb_tag_1_read","id text,school_commun_flag text,wire_tv_flag text",
// "gpfdist://mdw:8081/20190108.csv","CSV",";");
update("update tb_cp_02 set amt = ? where id = ?",new Object[]{"",""}); //查询
String selectSql = "select * from tb_cp_02";
ResultSet rs = query(selectSql);
List<TbInfo> tbInfos = resultSetToList(rs);
Iterator it = tbInfos.iterator();
while(it.hasNext()) {
Map hm = (Map)it.next();
System.out.println(hm.get("id"));
System.out.println(hm.get("amt"));
System.out.println(hm.get("date"));
}
closeConnection();
} catch (Exception e) {
e.printStackTrace(); }
}
}

JDBC连接Greenplum数据库,封装了增删改查的更多相关文章

  1. 【Python + Mysql】之用pymysql库连接Mysql数据库并进行增删改查操作

    用pip下载pymysql并引用 具体请参考文章: <Python之MySQL数据库增删改查操作> <python3.6 使用 pymysql 连接 Mysql 数据库及 简单的增删 ...

  2. .net 连接SqlServer数据库及基本增删改查

    一.写在前面 因为这学期选修的 .net 课程就要上机考试了,所以总结下.net 操作 SqlServer 数据的方法.(因为本人方向是 Java,所以对.net 的了解不多,但以下所写代码均是经过测 ...

  3. java连接mysql数据库 三 实现增删改查操作

    同以前一样,先写一个数据库打开和关闭操作类 public class DBConnection { String driver = "com.mysql.jdbc.Driver"; ...

  4. ORM 实现数据库表的增删改查

    这次通过反射技术来实现一下数据库表的增删改查对象关系映射(英语:Object Relational Mapping,简称ORM,或O/RM,或O/R mapping) 注:引用时约束了以下几点: 数据 ...

  5. Python进阶----数据库的基础,关系型数据库与非关系型数据库(No SQL:not only sql),mysql数据库语言基础(增删改查,权限设定)

    day37 一丶Python进阶----数据库的基础,mysql数据库语言基础(增删改查,权限设定) 什么是数据库:    简称:DataBase ---->DB    数据库即存放数据的仓库, ...

  6. 十四:SpringBoot-配置MongoDB数据库,实现增删改查逻辑

    SpringBoot-配置MongoDB数据库,实现增删改查逻辑 1.MongoDB数据库 1.1 MongoDB简介 1.2 MongoDB特点 2.SpringBoot整合MongoDB 2.1 ...

  7. Android(java)学习笔记245:ContentProvider使用(银行数据库创建和增删改查的案例)

    1. Android的四大组件: (1)Activity  用户交互的UI界面 (2)Service  后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...

  8. Flutter数据库Sqflite之增删改查

    Flutter数据库Sqflite之增删改查   简介 sqflite是Flutter的SQLite插件,支持iOS和Android,目前官方版本是sqflite1.1.3 sqflite插件地址:h ...

  9. Vc数据库编程基础MySql数据库的表增删改查数据

    Vc数据库编程基础MySql数据库的表增删改查数据 一丶表操作命令 1.查看表中所有数据 select * from 表名 2.为表中所有的字段添加数据 insert into 表名( 字段1,字段2 ...

  10. Android(java)学习笔记189:ContentProvider使用(银行数据库创建和增删改查的案例)

    1. Android的四大组件: (1)Activity  用户交互的UI界面 (2)Service  后台运行的服务 (3)BroadcastReceiver 广播接收者 (4)ContentPro ...

随机推荐

  1. [react001] 使用webpack自动构建react 项目

    1.react 简介 React 是一个Facebook出品的前端UI开发框架.react官方的tutorials 为了让人容易上手,并没有给在平常工作使用react的详细配置,随意学习的深入,你为了 ...

  2. solrconfig.xml配置详解

    solrconfig.xml配置文件主要定义了SOLR的一些处理规则,包括索引数据的存放位置,更新,删除,查询的一些规则配置. 可以在tomcat的安装路径下找到这个文件C:\Program File ...

  3. 使用Base64进行string的加密和解密

    //字符串转bytes var ebytes = System.Text.Encoding.Default.GetBytes(keyWord); //bytes进行base64加密 var strBa ...

  4. JPA_映射双向多对多的关联关系(转)

    双向多对多的关联关系 转自(http://www.cnblogs.com/lj95801/p/5011537.html) 双向多对多的关联关系(抽象成A-B)具体体现:A中有B的集合的引用,同时B中也 ...

  5. Android 获取模拟器与真机数据库

    模拟器: localuser:~ localhost$ adb shell shell@android:/ $ su // 数据库复制到 Download 下 shell@android:/ # cp ...

  6. MySQL大数据量的导入

    最近在公司备份数据库数据,简单的看了一下.当然我用的是简单的手动备份. 第一:其实最好的方法是直接用: mysqldump -u用户名 -p密码 数据库名 < 数据库名.sql 在linux在操 ...

  7. Aircrack-ng无线破解总结

    过年回来家,奈何没网,实属无奈,只好看破解教程,看能否破出来.于是总结如下 测试环境在linux平台下,我用的是ubuntu环境.ubuntu安装可以直接用sudo apt-get install a ...

  8. NOI2019省选模拟赛 第三场

    传送门 明明没参加过却因为点进去结果狂掉\(rating\)-- \(A\) 集合 如果我们记 \[f_k=\sum_{i=1}^nT^i{n-i\choose k}\] 那么答案显然就是\(f_{k ...

  9. django入门-初窥门径-part1

    尊重作者的劳动,转载请注明作者及原文地址 http://www.cnblogs.com/txwsqk/p/6510917.html 完全翻译自官方文档 https://docs.djangoproje ...

  10. SDN定义网络

    http://edu.51cto.com/course/course_id-4466.html http://edu.51cto.com/course/course_id-4497.html