Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 为insert设置表nem的准备语句_Mysql_Prepared Statement - Fatal编程技术网

Mysql 为insert设置表nem的准备语句

Mysql 为insert设置表nem的准备语句,mysql,prepared-statement,Mysql,Prepared Statement,我有一些准备好的声明,它们正在做相同的事情,但放在不同的桌子上: ReportImageWrongLang = con.prepareStatement("INSERT INTO userReportedWrongLang VALUES (?,?,?) ON DUPLICATE KEY UPDATE UserId=UserID"); ReportImageProhibited = con.prepareStatement("INSERT INTO userReportedImages VALUE

我有一些准备好的声明,它们正在做相同的事情,但放在不同的桌子上:

ReportImageWrongLang = con.prepareStatement("INSERT INTO userReportedWrongLang VALUES (?,?,?) ON DUPLICATE KEY UPDATE UserId=UserID");
ReportImageProhibited = con.prepareStatement("INSERT INTO userReportedImages VALUES (?,?,?) ON DUPLICATE KEY UPDATE UserId=UserID");
ReportElse=con.prepareStatement(“在重复密钥更新UserId=UserId上插入ReportElse值(?,?)

我根据java程序中的某个事件调用此语句

我想将这3条语句更改为一条语句:

Report = con.prepareStatement("INSERT INTO ? VALUES (?,?,?) ON DUPLICATE KEY UPDATE UserId=UserID");
并将第一个参数设置为表名(Report.setString(1,tableName))

但我得到了一个错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''userreportedimages' VALUES ('3',331,1) ON DUPLICATE KEY UPDATE UserId=UserID' at line 1

有什么办法吗?

您不能参数化SQL标识符,例如表名或列名

但是,您可以编写一个Java函数,将表名或列名作为参数,并将其连接到SQL字符串中。不过,要小心确保对其进行了消毒,以防止SQL注入攻击


这就是说,需要这样做通常是一个严重非规范化模式的迹象。具有相同结构的多个表通常可以(也应该)组合到一个表中,并使用一列指示两者之间的差异。

好的,谢谢,我将再次重新考虑我的模式