使用预处理语句设置表名

2022-01-30 00:00:00 sql prepared-statement java

我正在尝试使用准备好的语句来设置表名以从中选择数据,但是在执行查询时我不断收到错误消息.

I'm trying to use prepared statements to set a table name to select data from, but I keep getting an error when I execute the query.

错误和示例代码如下所示.

The error and sample code is displayed below.

[Microsoft][ODBC Microsoft Access Driver] Parameter 'Pa_RaM000' specified where a table name is required.



private String query1 = "SELECT plantID, edrman, plant, vaxnode FROM [?]"; //?=date
public Execute(String reportDate){
    try {

        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection conn = DriverManager.getConnection(Display.DB_MERC);
        PreparedStatement st = conn.prepareStatement(query1);
        st.setString(1, reportDate);
        ResultSet rs = st.executeQuery();

有什么可能导致这种情况的想法吗?

Any thoughts on what might be causing this?

推荐答案

表名不能作为参数.它必须是硬编码的.所以你可以这样做:

A table name can't be used as a parameter. It must be hard coded. So you can do something like:

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

相关文章