2

我在这个函数中得到一个错误,但我不知道为什么。你能帮助我吗?

错误在我打电话的那一行executeQuery()

public static int numeroElementi(String table) throws SQLException {

    // viene reistanziata perché questa è una funziona statica!
    String DB_URL = "jdbc:mysql://localhost:3306/kmzero";
    Connection connection = DriverManager.getConnection(DB_URL, "root", "root");

    String query = "SELECT COUNT(*) AS count FROM ? ";
    PreparedStatement pStatement = connection.prepareStatement(query);
    pStatement.setString(1, table);

    try {

        ResultSet resultSet = pStatement.executeQuery();

        try {
            if (resultSet.next())
                return resultSet.getInt("count");

            else
                return 0;

        } finally {
            resultSet.close();
        }
    } finally {
        pStatement.close();
    }
}
4

1 回答 1

7

我认为您不能将tablename也是 columnNames)作为参数传递,这是应该在您的字符串中连接tableName的时候。

String query = "SELECT COUNT(*) AS count FROM " + table;

如果你的桌子需要逃跑,

String query = "SELECT COUNT(*) AS count FROM `" + table + "`";
于 2012-11-17T15:44:13.713 回答