gpt4 book ai didi

java - Hibernate setParameter 向字符串添加单引号

转载 作者:行者123 更新时间:2023-12-01 06:23:00 25 4
gpt4 key购买 nike

我正在尝试使用 hibernate 准备好的语句创建新表。看起来 "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

最佳答案

您不能在准备好的语句中使用表名称作为参数。你必须把它放入刺中:

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

关于java - Hibernate setParameter 向字符串添加单引号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39014774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com