赞
踩
-
- Connection connection = null;
- PreparedStatement preparedStatement = null;
- ResultSet resultSet = null;
- try {
- // 1.注册驱动
- Class.forName("com.mysql.cj.jdbc.Driver");
- // 2.创建连接
- connection = DriverManager.getConnection(
- "jdbc:mysql://hadoop102:3306/gmall_realtime_config?" +
- "user=root&password=000000&useUnicode=true&" +
- "characterEncoding=utf8&serverTimeZone=Asia/Shanghai&useSSL=false");
- // 3.预编译sql,获取数据库操作对象
- preparedStatement = connection.prepareStatement(
- "select * from gmall_realtime_config.table_process where sink_type = 'dwd'");
- // 4.执行sql查询,获取数据集
- resultSet = preparedStatement.executeQuery();
- // 5.获取元数据信息
- ResultSetMetaData metaData = resultSet.getMetaData();
- // 6.处理查询结果。将查询信息 写入到 hashmap中
- while (resultSet.next()){
- JSONObject jsonObject = new JSONObject();
- for (int i = 1; i <= metaData.getColumnCount(); i++) {
- String columnName = metaData.getColumnName(i);
- String columnValue = resultSet.getString(i);
- jsonObject.put(columnName,columnValue);
- }
- TableProcess tableProcess = JSON.toJavaObject(jsonObject, TableProcess.class);
- if (tableProcess != null){
- String sourceTable = tableProcess.getSourceTable();
- String sourceType = tableProcess.getSourceType();
- String putKey = sourceTable + ":" + sourceType;
- hashMap.put(putKey,tableProcess);
- }
- }
- } catch (Exception e) {
- throw new RuntimeException(e);
- } finally {
- // 7.关闭连接
- if (resultSet!= null) resultSet.close();
- if (preparedStatement != null) preparedStatement.close();
- if (connection != null) connection.close();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。