java preparedStatement占位符问题

占位符,只是用来替代值,不能用来替代表名

ps = conn.prepareStatement("select * from "+ tableName);
//prep.setString(1, tableName);//运行报错
ResultSet result = ps.executeQuery();

实际上也可以理解:
就比如这句:

select * from t_user where name=?;

执行ps.setString(1,"张三");后变成:

select * from t_user where name='张三';

用在表名上会变成:

select * from 'xxx';//多了单引号
posted @ 2021-09-26 15:29  热心邻居老王  阅读(445)  评论(0编辑  收藏  举报