java统计数据库字段
package db; import java.sql.*; import java.util.ArrayList; import java.util.List; /** * @Author : dominic * */ public class Statistic {
public static void main(String[] args) throws SQLException, ClassNotFoundException {
String a = "xxx";// 选择什么数据库
String[] q = new String[500];
String[] w = new String[100];
List<String> list = new ArrayList<>();
int t=0;
Class.forName("com.mysql.cj.jdbc.Driver");
Connection aa = DriverManager.getConnection("jdbc:mysql://" + a + "",
"xxx", "xx");
Statement ff = aa.createStatement();
Statement qq = aa.createStatement();
Statement www = aa.createStatement();
Statement filed = aa.createStatement();
ResultSet g = ff.executeQuery("Show tables");// 查询此数据库有多少个表格
System.out.println("g = " + g);
String db;
int dbNo =0;
while(g.next()){
dbNo++;
db = g.getString(1);
// System.out.println("db "+g.getString(1));
//把表名给赋值给数组
q[t]=db;
//以数组来查一下此表格的列
ResultSet u=qq.executeQuery("DESC "+q[t]+"");
int l=0;
list = new ArrayList<>();
while(u.next()){
String string = u.getString(1);
list.add(string);
// w[l++]=u.getString(1);//获取此表的列数
}
ResultSet y = www.executeQuery("select count(*) from " + q[t++] + "");//查询此表的数据
Integer count = null;
while (y.next()) {//获取此表数据
/*for (int i = 1; i < l + 1; i++) {//以列的数值为终止行,来打印!
}*/
count = y.getInt(1);
StringBuffer buffer = new StringBuffer();
for (String s : list) {
String a1="`%s`";
String sql2 = "select * from " + db + " where " + a1 + " REGEXP" + " \'(https|http)\'" + " limit 1";
String sql = String.format(sql2,s);
ResultSet resultSet = filed.executeQuery(sql);
if (resultSet.next()){
buffer.append(" ").append(s).append("-").append(1);
}else {
buffer.append(" ").append(s).append("-").append(0);
}
}
String outDbData = db + " " + count + " " + buffer;
System.out.println(outDbData);
}
}
System.out.println("dbNo = " + dbNo);
g.close();
ff.close();
aa.close();
}
}
不积跬步,无以至千里;不积小流,无以成江海。