Java如何实现获得MySQL数据库中所有表的记录总数可行方法

我们一生之中,要牢记和要忘记的东西一样多。其实人生最好的贵人,就是不断努力的自己!靠自己,最踏实!假期很短暂,快乐却无边,工作很苦闷,赚钱最重要,陶醉在假期的快乐中,奋斗在工作的战线中,苦中寻乐一点甜,快乐与你长相伴。
在MySQL中,可以通过SELECT COUNT(*) FROM table_name查询某个表中有多少条记录。如果想知道某个数据库中所有别的记录总数应该怎么做呢?本文给出两种可行的Java程序,解决该问题。1. 首先确定数据库中有多少个表,然后对每个表执行SELECT COUNT(*) FROM table_name
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Test {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1/";
private static String db = "test";
private static String user = "root";
private static String pass = "test";
static Connection conn = null;
static Statement statement = null;
static PreparedStatement ps = null;
static ResultSet rs = null; static List<String> tables = new ArrayList<String>(); public static void startMySQLConn() {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+db, user, pass);
if (!conn.isClosed()) {
System.out.println("Succeeded connecting to MySQL!");
} statement = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
} public static void closeMySQLConn() {
if(conn != null){
try {
conn.close();
System.out.println("Database connection terminated!");
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void getTables() {
String sql = "show tables;";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
tables.add(rs.getString(1));
}
} catch (Exception e) {
e.printStackTrace();
}
} public static long getDbSum() {
long sum = 0;
String sql = "select count(*) from ";
try {
for(String tblName: tables) {
ps = conn.prepareStatement(sql + tblName + ";");
rs = ps.executeQuery();
while (rs.next()) {
sum += rs.getInt(1);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return sum;
} public static void main(String[] args) {
startMySQLConn();
getTables();
System.out.println(getDbSum());
closeMySQLConn();
}
}

2. 借助information_schema库的tables表
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class Test {
private static String driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1/";
private static String db = "test";
private static String user = "root";
private static String pass = "test";
static Connection conn = null;
static Statement statement = null;
static PreparedStatement ps = null;
static ResultSet rs = null; public static void startMySQLConn() {
try {
Class.forName(driver).newInstance();
conn = DriverManager.getConnection(url+db, user, pass);
if (!conn.isClosed()) {
System.out.println("Succeeded connecting to MySQL!");
} statement = conn.createStatement();
} catch (Exception e) {
e.printStackTrace();
}
} public static void closeMySQLConn() {
if(conn != null){
try {
conn.close();
System.out.println("Database connection terminated!");
} catch (SQLException e) {
e.printStackTrace();
}
}
} public static void useDB() {
String sql = "use information_schema;";
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
} catch (Exception e) {
e.printStackTrace();
}
} public static long getDbSum() {
long sum = 0;
String sql = "select table_name,table_rows from tables where TABLE_SCHEMA = '" +
db + "' order by table_rows desc;";
//System.out.println(sql);
try {
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
while (rs.next()) {
sum += rs.getInt(2);
}
} catch (Exception e) {
e.printStackTrace();
}
return sum;
} public static void main(String[] args) {
startMySQLConn();
useDB();
System.out.println(getDbSum());
closeMySQLConn();
}
}

到此这篇关于Java如何实现获得MySQL数据库中所有表的记录总数可行方法就介绍到这了。任何事情,坚持了就是神话,放弃了就是笑话!这个道理听起来很简单,但很多人却做不到,不停的选择,不停的放弃,回头却发现什么事都没做好,坚持,一定能遇到最美的自己,送给正在努力拼搏的我们!更多相关Java如何实现获得MySQL数据库中所有表的记录总数可行方法内容请查看相关栏目,小编编辑不易,再次感谢大家的支持!

您可能有感兴趣的文章
mysql数据库中getshell的方式总结

数据库中的sql完整性约束语句解析

MySQL数据库8——数据库中函数的应用详解

MySQL数据库中CAST与CONVERT函数如何实现类型转换的讲解

将图片储存在MySQL数据库中的几种方法