我在这个函数中出错,但我不知道为什么。 你能救我吗?
错误出现在我调用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();
}
}
答案 0 :(得分:7)
我认为您无法将 tablename ( also columnNames )作为参数传递,这是 tableName 应该在您的网络中连接的时间字符串。
String query = "SELECT COUNT(*) AS count FROM " + table;
如果你的桌子需要逃脱,
String query = "SELECT COUNT(*) AS count FROM `" + table + "`";