1、获取catalog
1)代码如下:
/**
* 获取catalog
*
* @param jdbcdriver 驱动类(DriverClass)(com.mysql.cj.jdbc.Driver)
* @param url 地址(jdbc:mysql://10.20.30.40:3306)
* @param username 用户名
* @param password 密码
*/
public static List<String> getCatalogs(String jdbcdriver, String url, String username, String password) throws ClassNotFoundException, SQLException {
ArrayList<String> list = new ArrayList<>();
// 1、加载驱动
Class.forName(jdbcdriver);
// 2、建立连接
Connection conn = DriverManager.getConnection(url, username, password);
try {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet resultSet = metaData.getCatalogs();
while (resultSet.next()) {
String string = resultSet.getString("TABLE_CAT");
list.add(string);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
return list;
}
2)main方法调用
public static void main(String[] args) throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306";
String user = "root";
String password = "root3306";
//获取catalog
List<String> catalogs = getCatalogs(driver, url, user, password);
catalogs.stream().forEach(item-> System.out.println(item));
}
3)请求结果:
2、获取表
1)代码如下:
/**
* 获取表
*
* @param jdbcdriver 驱动类(DriverClass)(com.mysql.cj.jdbc.Driver)
* @param url 地址(jdbc:mysql://10.20.30.40:3306)
* @param username 用户名
* @param password 密码
* @param catalogs catalogs
* @param database 数据库
*/
public static List<String> getTables(String jdbcdriver, String url, String username, String password, String catalogs, String database) throws ClassNotFoundException, SQLException {
List<String> list = new ArrayList<>();
// 1、加载驱动
Class.forName(jdbcdriver);
// 2、建立连接
Connection conn = DriverManager.getConnection(url, username, password);
try {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet tables = metaData.getTables(
catalogs,
database,
"%",
new String[]{"TABLE", "VIEW"});
while (tables.next()) {
String name = tables.getString("TABLE_NAME");
list.add(name);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
conn.close();
}
return list;
}
2)main方法调用
public static void main(String[] args) throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306";
String user = "root";
String password = "root3306";
//获取表名
List<String> tables = getTables(driver, url, user, password, "mybatis_plus", "mybatis_plus");
tables.stream().forEach(item-> System.out.println(item));
}
3)请求结果:
3、获取字段
1)代码如下:
/**
* 获取字段
*
* @param jdbcdriver 驱动类(DriverClass)(com.mysql.cj.jdbc.Driver)
* @param url 地址(jdbc:mysql://10.20.30.40:3306)
* @param username 用户名
* @param password 密码
* @param catalogs catalogs
* @param database 库
* @param table 表
*/
public static List<JSONObject> getColumns(String jdbcdriver, String url, String username, String password, String catalogs, String database, String table) throws ClassNotFoundException, SQLException, SQLException {
List<JSONObject> list = new ArrayList<>();
// 1、加载驱动
Class.forName(jdbcdriver);
// 2、建立连接
Connection conn = DriverManager.getConnection(url, username, password);
try {
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getColumns(catalogs, database, table, "%");
while (rs.next()) {
JSONObject json = new JSONObject();
// 列明
json.put("columnName", toCamelCase(rs.getString("COLUMN_NAME")));
// 列类型
json.put("typeName", rs.getString("TYPE_NAME"));
// 列备注
json.put("remarks", rs.getString("REMARKS"));
list.add(json);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}
return list;
}
2)main方法调用
public static void main(String[] args) throws Exception {
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://127.0.0.1:3306";
String user = "root";
String password = "root3306";
//获取字段
List<JSONObject> columns = getColumns(driver, url, user, password, "mybatis_plus", "mybatis_plus", "sys_user");
columns.stream().forEach(item-> System.out.println(item));
}
3)请求结果:
4、实现驼峰式(类名)命名和下划线(数据库)命名之间的转化
1)下划线转驼峰
/**
* 下划线转驼峰
*/
public static String toCamelCase(String s) {
if (s == null) {
return null;
}
//s = s.toLowerCase();
StringBuilder sb = new StringBuilder(s.length());
boolean upperCase = false;
for (int i = 0; i < s.length(); i++) {
// 找字符
char c = s.charAt(i);
// 如果是下划线
if (c == '_') {
// 将大写设置为true
upperCase = true;
} else if (upperCase) {
// 下次转换首字母
sb.append(Character.toUpperCase(c));
// 将大写设置为false
upperCase = false;
} else {
// 追加
sb.append(c);
}
}
return sb.toString();
}
2)驼峰转下划线
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 驼峰转下划线
*/
public static String ToUnderScoreCaseString(String s) {
if (s == null) {
return null;
}
Pattern compile = Pattern.compile("[A-Z]");
Matcher matcher = compile.matcher(s);
StringBuffer sb = new StringBuffer();
while(matcher.find()) {
//将匹配到的大写字符转换成小写,并且在前面添加下划线然后添加到缓冲区。group(0)在没有分配组的时候匹配所有符合的
matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
}
matcher.appendTail(sb);
return sb.toString();
}