package com.saga.hbase.phoenix.app; import com.saga.hbase.phoenix.constant.PhoenixConstant; import java.sql.*; public class PhoenixDML { private static Connection connection = null; private static Statement statement = null; static { System.out.println("init phoenix config ... "); try { Class.forName(PhoenixConstant.PHOENIX_DRIVER); connection = DriverManager.getConnection(PhoenixConstant.PHOENIX_SERVER); statement = connection.createStatement(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } } public static void main(String[] args) throws ClassNotFoundException, SQLException { delete(); closeConnection(); } //TODO 查询 public static void select(String schema, String tableName) throws ClassNotFoundException, SQLException { Class.forName(PhoenixConstant.PHOENIX_DRIVER); Connection connection = DriverManager.getConnection(PhoenixConstant.PHOENIX_SERVER); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery("select * from SYSTEM.US_POPULATION"); int columnCount = resultSet.getMetaData().getColumnCount(); while (resultSet.next()){ for (int i = 1; i <= columnCount; i++) { String state = resultSet.getString(i); System.out.print(state + " "); } System.out.println(); } } //TODO 更新插入 public static Integer upsert() throws SQLException { String sql1="upsert into SAGA_LOG.test_phoenix_api values(1,'test1')"; String sql2="upsert into SAGA_LOG.test_phoenix_api values(2,'test2')"; String sql3="upsert into SAGA_LOG.test_phoenix_api values(3,'test3')"; statement.executeUpdate(sql1); statement.executeUpdate(sql2); statement.executeUpdate(sql3); connection.commit(); return 0; } //TODO 插入 public Integer insert(){ return 0; } //TODO 删除 public static Integer delete() throws SQLException { String sql1="delete from SAGA_LOG.test_phoenix_api where mykey = 1"; statement.executeUpdate(sql1); connection.commit(); return 0; } public static void closeConnection(){ try { if (statement != null) statement.close(); if (connection != null) connection.close(); } catch (SQLException e) { e.printStackTrace(); } } }