方式一:mysql适用
public List<JSONObject> getDbTableDetail(JSONObject connectConfig) throws Exception {
Class.forName(connectConfig.getString("driver"));
String url = connectConfig.getString("url");
Connection connection = DriverManager.getConnection(url, connectConfig.getString("user"), connectConfig.getString("password"));
ResultSet rs = connection.getMetaData().getColumns("", "", connectConfig.getString("table"), null);
List<JSONObject> list = new ArrayList<>();
while (rs.next()) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("DATA_TYPE", rs.getInt("DATA_TYPE"));
jsonObject.put("COLUMN_SIZE", rs.getInt("COLUMN_SIZE"));
jsonObject.put("COLUMN_NAME", rs.getString("COLUMN_NAME"));
jsonObject.put("NULLABLE", rs.getInt("NULLABLE") == 1);
jsonObject.put("DECIMAL_DIGITS", rs.getInt("DECIMAL_DIGITS"));
jsonObject.put("REMARKS", rs.getString("REMARKS"));
jsonObject.put("COLUMN_DEF", rs.getString("COLUMN_DEF"));
jsonObject.put("TABLE_CAT", rs.getString("TABLE_CAT"));
jsonObject.put("TABLE_SCHEM", rs.getString("TABLE_SCHEM"));
jsonObject.put("TABLE_NAME", rs.getString("TABLE_NAME"));
list.add(jsonObject);
}
connection.close();
return list;
}
方式二:mysql、clickhouse等适用
通过jdbc 连接 执行SQL语句 DESCRIBE TABLE_NAME
@RequestMapping(value = "/test", method = RequestMethod.POST, consumes = "text/plain")
public List<Map<String, String>> suppliertest(@RequestBody String sql) {
return selectall = ckMapper.selectAllTest(sql);
}
public interface ckMapper {
List<Map<String, String>> selectAllTest(@Param("sql") String sql);
}
ckMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.dao.ckMapper">
<select id="selectAllTest" resultType="java.util.HashMap">
${sql}
</select>
</mapper>
|