【发布时间】:2014-04-21 14:43:38
【问题描述】:
所以我尝试使用JDBC 获取mysql 表中存在的行数,但使用第一列作为参考,因为这是存在的行数,但此列位于AUTO_INCREMENT 但是,假设n rows从表中删除,然后一些新的是inserted,新的行号自然会保持相同的顺序计数,当我想计算表的行数时有,我的getTableInfo 方法返回行数,包括被删除的行数。我该如何解决这个问题?可以这样做吗?
public int getTableInfo(String tableName, String db, Boolean columnCount,
Boolean rowCount) {
TABLE = tableName;
Connection getTableInfoConn = this.getConnect_to_DB(db);
try {
PreparedStatement prepStatement = getTableInfoConn
.prepareStatement("SELECT * FROM `" + tableName + "`");
ResultSet res = prepStatement.executeQuery();
if ((columnCount == false) && (rowCount == false)
|| (columnCount == null) && (rowCount == false)
|| (columnCount == false) && (rowCount == null)
|| (columnCount == null) && (rowCount == null)) {
JOptionPane.showMessageDialog(null,
"uno de los booleanos debe ser TRUE", null,
JOptionPane.ERROR_MESSAGE, null);
} else {
if (columnCount == false && rowCount == true
|| columnCount == null) {
while (res.next()) {
resultMetaData = res.getInt(1);
}
} else if (columnCount == true && rowCount == false
|| rowCount == null) {
metaData = res.getMetaData();
resultMetaData = metaData.getColumnCount();
}
}
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, "Error in getTableInfo():" + e);
}
return resultMetaData;
}
【问题讨论】:
-
仅供参考:如果
tableName来自用户,则此代码会受到 SQL 注入。 -
是的,谢谢,我还在学习,但我会尽快实现。