Home >Java >javaTutorial >How Can I Use a Parameterized Table Name in a SQL Query?

How Can I Use a Parameterized Table Name in a SQL Query?

Patricia Arquette
Patricia ArquetteOriginal
2024-12-13 21:42:10624browse

How Can I Use a Parameterized Table Name in a SQL Query?

Parameterized Table Name for Data Retrieval

When attempting to utilize prepared statements to define a table name for data retrieval, users often encounter errors. This issue arises when a parameter is used in place of a required table name.

Consider the following example:

private String query1 = "SELECT plantID, edrman, plant, vaxnode FROM [?]"; // ?=date

This query attempts to use a parameter (?) to represent the table name, which is not supported. Instead, the table name must be hard-coded.

To resolve this issue, declare the table name explicitly in the query:

private String query1 = "SELECT plantID, edrman, plant, vaxnode FROM [" + reportDate + "]?"";

This modification ensures that the table name is properly specified, enabling successful execution of the query.

The above is the detailed content of How Can I Use a Parameterized Table Name in a SQL Query?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn