java.sql.SQL语法错误尝试更新字符串字段时出现异常

我在phpMyAdmin中设置了我的数据库,在下面的查询中,我正在尝试更新包含某些软件的文件路径的字符串字段,例如“C:\Program Files (x86)\卡巴斯基实验室\卡巴斯基全方位安全软件19.0.0\avp.exe”。


当我执行查询时,我得到java.sql.SQLSyntax错误异常,如下所示。此查询在 phpMyAdmin 本身中运行时工作正常。我做错了什么?


我的查询:


String query4 = MessageFormat.format("UPDATE system_object SET file_path = {0}, validate={1} " +

            "where category_key = 2", settings.getAntivirus_filePath(), settings.isAntivirus_validate());

编辑我确实尝试过在我的参数上使用引号,但后来我只是得到同样的错误

java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0}, validate=true where category_key = 2' at line 1


jeck猫
浏览 105回答 3
3回答

慕少森

使用 a 和绑定参数;您当前的方法容易受到 sql 注入的影响。忽略这种担忧(这是一个很大的问题),你没有引用你的参数。你可以做PreparedStatementString query4 = MessageFormat.format(        "UPDATE system_object SET file_path = '{0}', validate='{1}' " +        "where category_key = 2", settings.getAntivirus_filePath(),         settings.isAntivirus_validate());

慕姐8265434

将代码修改为:String query4 = MessageFormat.format(            "UPDATE system_object SET file_path = \"{0}\", validate=\"{1}\" " +            "where category_key = 2", "demo1",             "demo2");    System.out.println(query4);输出:UPDATE system_object SET file_path = "demo1", validate="demo2" where category_key = 2

烙印99

只是一个更新使用双''而不是单':String query4 = MessageFormat.format(        "UPDATE system_object SET file_path = '{0}', validate='{1}' " +        "where category_key = 2", settings.getAntivirus_filePath(),         settings.isAntivirus_validate());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java