【问题标题】:Hibernate setParameter adds single quote to stringHibernate setParameter 将单引号添加到字符串
【发布时间】:2023-04-09 05:47:01
【问题描述】:

我正在尝试使用休眠准备语句创建新表。看起来"setparameter("values", value)" 为查询添加了额外的引号。 我的代码:

String hql = "create table :values " + "(name VARCHAR(50))";
        Query my = session.createSQLQuery(hql);
        my.setParameter("values", value);
        my.executeUpdate();
        session.close();

错误:

SEVERE: Servlet.service() for servlet [contextServlet] in context with path [/SpringSafeHouseService2.0] threw exception [Request processing failed; nested exception is org.hibernate.exception.SQLGrammarException: could not execute statement] with root cause
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 ''merkurijuss' (name VARCHAR(50))' at line 1

【问题讨论】:

    标签: java spring hibernate error-handling prepared-statement


    【解决方案1】:

    您不能在准备好的语句中使用表名作为参数。您必须将其放入刺痛中:

        String hql = "create table "+ value+ " (name VARCHAR(50))";
        Query my = session.createSQLQuery(hql);
        my.executeUpdate();
        session.close();
    

    【讨论】:

    • 那么如何防止这个查询被SQL注入呢?
    • @☺Liver SQL 注入仅适用于列的值而不是表名
    • 所以这个查询对于 SQL 注入是完全安全的? String hql = "创建表 "+ value+ " (name VARCHAR(50))";
    猜你喜欢
    • 2013-03-09
    • 1970-01-01
    • 1970-01-01
    • 2022-08-17
    • 2019-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    相关资源
    最近更新 更多