java在jdbc元数据中获取columnCount=1
表中的列数不断出错我做错了什么这是代码 这不对吗?我做错了什么?如何获得特定表的列数?:
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 COUNT(*) 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;
}
主要方法是=
public static void main(String[] args) {
ConectorBaseDatos dbconector = new ConectorBaseDatos();
int numFilas = dbconector.getTableInfo("arbitros","fifa", false, true);
System.out.println("Numero de filas de arbitros es : "+ numFilas);
int numColumnas = dbconector.getTableInfo("arbitros", "fifa", true, false);
System.out.println("numero de columnas es : " + numColumnas);
}
为什么columnCount = 1
# 1 楼答案
您在这里发出的查询是:
结果将是一列、一行,只保留“arbitros”中的行数。该结果只有一列(名为“count(*)或类似内容)
如果需要arbitros中的列数,需要的查询是: