|
@@ -1,36 +1,65 @@
|
|
|
package com.saga.hbase.phoenix.app;
|
|
|
|
|
|
-import com.saga.hbase.phoenix.bean.UsPopulation;
|
|
|
import com.saga.hbase.phoenix.constant.PhoenixConstant;
|
|
|
|
|
|
-import java.lang.reflect.Field;
|
|
|
import java.sql.*;
|
|
|
-import java.util.List;
|
|
|
|
|
|
|
|
|
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 {
|
|
|
+
|
|
|
+ select("", "");
|
|
|
+ closeConnection();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
//TODO 查询
|
|
|
- public static <T> List<T> select(String schema, String tableName, T t) throws ClassNotFoundException, SQLException {
|
|
|
+ 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");
|
|
|
- Field[] fields = t.getClass().getDeclaredFields();
|
|
|
|
|
|
+ int columnCount = resultSet.getMetaData().getColumnCount();
|
|
|
while (resultSet.next()){
|
|
|
- UsPopulation usPopulation = new UsPopulation();
|
|
|
-
|
|
|
- for (Field field : fields) {
|
|
|
- String state = resultSet.getString(field.getName());
|
|
|
+ for (int i = 1; i <= columnCount; i++) {
|
|
|
+ String state = resultSet.getString(i);
|
|
|
+ System.out.print(state + " ");
|
|
|
}
|
|
|
+
|
|
|
+ System.out.println();
|
|
|
}
|
|
|
- return null;
|
|
|
}
|
|
|
|
|
|
//TODO 更新
|
|
|
public Integer update(){
|
|
|
+ String sql1="upsert into test_phoenix_api values(1,'test1')";
|
|
|
+ String sql2="upsert into test_phoenix_api values(2,'test2')";
|
|
|
+ String sql3="upsert into test_phoenix_api values(3,'test3')";
|
|
|
+// stat.executeUpdate(sql1);
|
|
|
+// stat.executeUpdate(sql2);
|
|
|
+// stat.executeUpdate(sql3);
|
|
|
+// conn.commit();
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
@@ -43,4 +72,16 @@ public class PhoenixDML {
|
|
|
public Integer delete(){
|
|
|
return 0;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public static void closeConnection(){
|
|
|
+ try {
|
|
|
+ if (statement != null)
|
|
|
+ statement.close();
|
|
|
+ if (connection != null)
|
|
|
+ connection.close();
|
|
|
+ } catch (SQLException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|