Using Column Names as Input Parameters in PreparedStatements
Question:
Can a column name be used as an input parameter in a PreparedStatement query, such as "SELECT * FROM A,B WHERE A.X = ?" and assigning a value to B.Y as the parameter?
Answer:
No, JDBC does not permit the use of column names as input parameters. PreparedStatements only accept column values as parameters. To modify the SQL statement dynamically, it must be done before creating the PreparedStatement.
Using the example provided:
Table A has Attribute X + others Table B has Attribute Y + others
The PreparedStatement can be constructed as follows:
String sql = "SELECT * FROM A, B WHERE A.X = ?"; PreparedStatement statement = connection.prepareStatement(sql); statement.setString(1, "value for A.X");
However, it is not possible to dynamically set the parameter to "B.Y" as it is a column name.
The above is the detailed content of Can Column Names Be Used as Input Parameters in PreparedStatements?. For more information, please follow other related articles on the PHP Chinese website!