在jdbc metaData中获取columnCount = 1

时间:2014-04-19 19:08:08

标签: java jdbc

我继续在表格中输入错误的列数我在这里做错了代码 这不正确吗?我究竟做错了什么 ?如何获取特定表具有的列数? :

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 个答案:

答案 0 :(得分:0)

您在此处发出的查询是:

select count(*) from arbitros;

结果将是单列,单行,只保留" arbitros"中的行数。该结果有一个列(名为" count(*)"或类似的东西)。

如果您想要仲裁中的列数,您需要的查询是:

select * from arbitros;