查询一个数据库中某个列名存在哪些表,哪些库
程序员文章站
2024-01-13 14:45:34
...
在工作中遇到这样一个场景,需要找出数据库中所有的电话号码的字段。因此写一个程序自动去获取,因为一个服务器中数据库大概有几十个,一个数据库的表平均也是几十张表,并不能通过人力去一个个查找。代码如下:
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.*;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/testdb")
public class Test {
@RequestMapping(value = "/getAllDataBases", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, method = RequestMethod.GET)
public void getAllDataBases() {
String prefixUrl = "jdbc:mysql://localhost:3306/";
String user = "root";
String password = "password";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306", user, password);
DatabaseMetaData meta = con.getMetaData();
ResultSet resultSet = meta.getCatalogs();
List<String> strings = new ArrayList<>();
while (resultSet.next()) {
String db = resultSet.getString("TABLE_CAT");
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUrl(prefixUrl + db);
dataSource.setUsername(user);
dataSource.setPassword(password);
Connection connection = dataSource.getConnection();
DatabaseMetaData databaseMetaData = connection.getMetaData();
String[] tableTypes = {"TABLE"};
ResultSet tableResultSet = databaseMetaData.getTables(connection.getCatalog(), null, null, tableTypes);
// System.out.println("===============数据库名称 : "+db+" ===============");
while (tableResultSet.next()) {
String table_name = tableResultSet.getString("TABLE_NAME");
// System.out.println(">>>>>>>>>>>>> 表名称 : "+table_name+" >>>>>>>>>>>>");
ResultSet columns = databaseMetaData.getColumns(connection.getCatalog(), null, table_name, null);
while (columns.next()) {
String column_name = columns.getString("COLUMN_NAME");
if (column_name.contains("mobile") || column_name.contains("phone") || StringUtils.equalsIgnoreCase("phone", column_name)) {
// System.out.println("数据库名称:"+db+", 表名称 :"+table_name);
String str = "数据库名称:" + db + ", 表名称 :" + table_name + ", 字段名:" + column_name;
strings.add(str);
}
}
columns.close();
}
tableResultSet.close();
connection.close();
dataSource.close();
}
resultSet.close();
con.close();
writeFile(strings);
} catch (Exception e) {
e.printStackTrace();
}
}
public void writeFile(List<String> strings) throws IOException {
//写入中文字符时解决中文乱码问题
File file = new File("D:/database.txt");
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
BufferedWriter bw = new BufferedWriter(osw);
for (String arr : strings) {
bw.write(arr + "\t\n");
}
//注意关闭的先后顺序,先打开的后关闭,后打开的先关闭
bw.close();
osw.close();
fos.close();
}
}
上一篇: 如何将本地项目上传到Github
下一篇: Mysql锁表问题解决过程