MySQL导出表结构相关字段以及把字段由下划线转驼峰命名


SELECT
COLUMN_COMMENT 中文名, UPPER(COLUMN_NAME) 字段名, UPPER(DATA_TYPE) 字段类型, CHARACTER_MAXIMUM_LENGTH 长度, IS_NULLABLE 是否为空 FROM INFORMATION_SCHEMA.COLUMNS where table_schema ='shwmm' AND table_name = 'gis_interface_veh_his_gps_data'

MySQL导出表结构相关字段以及把字段由下划线转驼峰命名_第1张图片

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedHashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.mysql.jdbc.Connection;

/**
 * 工具类
 * MySQL中以“_”分割的列名格式化为驼峰命名的列名
 */
public class FormatDbColumns {
    static final String DB_URL = "jdbc:mysql://localhost:3306/shwmm";
    // MySQL的JDBC URL编写方式:jdbc:mysql://主机名称:连接端口/数据库的名称
    static final String USER = "root";
    static final String PASS = "root";

    public static void main(String[] args) throws SQLException,Exception{
        Connection conn = null;
        Statement stat = null;

        // 注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        // 创建链接
        conn = (Connection) DriverManager.getConnection(DB_URL,USER,PASS);

        String str = "am_alarm_detail";

        // 执行查询
        stat = conn.createStatement();
        String sql = "select LOWER(COLUMN_NAME) as columnName from information_schema.COLUMNS where table_name = '"+str+"'";
        ResultSet rs = stat.executeQuery(sql);
        // 输出查询结果
        int i = 0;
        Map map = new LinkedHashMap<>();
        while(rs.next()){
            i++;
            String a = rs.getString("columnName");
            String b= camelCaseName(a);
            map.put(b,"xxx");
            System.out.println(b);
        }

        System.out.println(JSONObject.toJSONString(map));
        System.out.println(i);
        // 关闭
        try {
            if (rs != null) {
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                if (stat != null) {
                    stat.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (conn != null) {
                        conn.close();
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String camelCaseName(String underscoreName) {
        StringBuilder result = new StringBuilder();
        if (underscoreName != null && underscoreName.length() > 0) {
            boolean flag = false;
            for (int i = 0; i < underscoreName.length(); i++) {
                char ch = underscoreName.charAt(i);
                if ("_".charAt(0) == ch) {
                    flag = true;
                } else {
                    if (flag) {
                        result.append(Character.toUpperCase(ch));
                        flag = false;
                    } else {
                        result.append(ch);
                    }
                }
            }
        }
        return result.toString();

    }
}

 

转载于:https://www.cnblogs.com/zplogo/p/9871089.html

你可能感兴趣的:(MySQL导出表结构相关字段以及把字段由下划线转驼峰命名)