简体   繁体   中英

getting columnCount = 1 in jdbc metaData

I keep on getting wrong number of columns in table what am i doing wrong here's the code is this not correct ? what am i doing wrong ? how to I get the amount of columns that a particular table has then ? :

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;

}

and main method is =

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);
}

why is columnCount = 1 ?

The query that you're issuing here is:

select count(*) from arbitros;

The result is going to be a single column, single row, just holding the number of rows in "arbitros". That result has a single column (named "count(*)" or something similar).

If you want the number of columns in arbitros, the query you need is:

select * from arbitros;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM