java.sql.SQLSyntaxErrorException问题常见解决方案:比如Table xxx doesn‘t exist;Unknown column ‘xxx‘ in ‘where...

奋斗吧
奋斗吧
擅长邻域:未填写

标签: java.sql.sqlsyntaxerrorexception

2023-03-23 21:16:25 352浏览

解决 bad SQL grammar [xxx]; nested exception is java.sql.SQLSyntaxErrorException: 1. Table xxx doesn't exist 2. You have an error in your SQL syntax;3. Unknown column x in x 4.Unknown column 'xxx' in 'where clause'。

1. Table ‘jqp.spring_session’ doesn’t exist

我们有时在启动本地项目,或者启动git上下载的源码时,会报错如下错误:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM SPRING_SESSION WHERE EXPIRY_TIME < ?]; nested exception is java.sql.SQLSyntaxErrorException: Table 'jqp.spring_session' doesn't exist
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:960) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1015) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:1025) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.session.jdbc.JdbcIndexedSessionRepository.lambda$cleanUpExpiredSessions$8(JdbcIndexedSessionRepository.java:587) ~[spring-session-jdbc-2.6.1.jar:2.6.1]
	at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140) ~[spring-tx-5.3.14.jar:5.3.14]
	at org.springframework.session.jdbc.JdbcIndexedSessionRepository.cleanUpExpiredSessions(JdbcIndexedSessionRepository.java:587) ~[spring-session-jdbc-2.6.1.jar:2.6.1]
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.3.14.jar:5.3.14]
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:95) [spring-context-5.3.14.jar:5.3.14]
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_102]
	at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266) [na:1.8.0_102]
	at java.util.concurrent.FutureTask.run(FutureTask.java) [na:1.8.0_102]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_102]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [na:1.8.0_102]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [na:1.8.0_102]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [na:1.8.0_102]
	at java.lang.Thread.run(Thread.java:745) [na:1.8.0_102]
Caused by: java.sql.SQLSyntaxErrorException: Table 'jqp.spring_session' doesn't exist
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1098) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdateInternal(ClientPreparedStatement.java:1046) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeLargeUpdate(ClientPreparedStatement.java:1371) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.mysql.cj.jdbc.ClientPreparedStatement.executeUpdate(ClientPreparedStatement.java:1031) ~[mysql-connector-java-8.0.27.jar:8.0.27]
	at com.zaxxer.hikari.pool.ProxyPreparedStatement.executeUpdate(ProxyPreparedStatement.java:61) ~[HikariCP-4.0.3.jar:na]
	at com.zaxxer.hikari.pool.HikariProxyPreparedStatement.executeUpdate(HikariProxyPreparedStatement.java) ~[HikariCP-4.0.3.jar:na]
	at org.springframework.jdbc.core.JdbcTemplate.lambda$update$2(JdbcTemplate.java:965) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:651) ~[spring-jdbc-5.3.14.jar:5.3.14]
	... 16 common frames omitted

1.1 分析问题

bad SQL grammar [DELETE FROM SPRING_SESSION WHERE EXPIRY_TIME < ?]; nested exception is java.sql.SQLSyntaxErrorException: Table 'jqp.spring_session' doesn't exist,翻译成中文便是:错误的SQL语法[删除表SPRING_SESSION失败],因为jqp库中不存在spring_session表。

程序中为什么会报出这种错误呢?原来,我们在pom中引入了如下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

1.2 解决问题

既然知道了问题,那么,可以采用如下方式解决

1.2.1 第一种解决方法

这种解决方法就是在pom文件中,注释掉当前依赖。

1.2.2 第二种解决方式

上文不是说没有spring_session表吗,我们可以手动创建这这张表,如下代码所示:

DROP TABLE IF EXISTS SPRING_SESSION_ATTRIBUTES;
DROP TABLE IF EXISTS SPRING_SESSION;

-- 创建SPRING_SESSION表
CREATE TABLE SPRING_SESSION (
    PRIMARY_ID CHAR(36) NOT NULL,
    SESSION_ID CHAR(36) NOT NULL,
    CREATION_TIME BIGINT NOT NULL,
    LAST_ACCESS_TIME BIGINT NOT NULL,
    MAX_INACTIVE_INTERVAL INT NOT NULL,
    EXPIRY_TIME BIGINT NOT NULL,
    PRINCIPAL_NAME VARCHAR(100),
    CONSTRAINT SPRING_SESSION_PK PRIMARY KEY (PRIMARY_ID)
) ENGINE=INNODB ROW_FORMAT=DYNAMIC;

CREATE UNIQUE INDEX SPRING_SESSION_IX1 ON SPRING_SESSION (SESSION_ID);
CREATE INDEX SPRING_SESSION_IX2 ON SPRING_SESSION (EXPIRY_TIME);
CREATE INDEX SPRING_SESSION_IX3 ON SPRING_SESSION (PRINCIPAL_NAME);

-- 创建spring_session属性表
CREATE TABLE SPRING_SESSION_ATTRIBUTES (
    SESSION_PRIMARY_ID CHAR(36) NOT NULL,
    ATTRIBUTE_NAME VARCHAR(200) NOT NULL,
    ATTRIBUTE_BYTES BLOB NOT NULL,
    CONSTRAINT SPRING_SESSION_ATTRIBUTES_PK PRIMARY KEY (SESSION_PRIMARY_ID, ATTRIBUTE_NAME),
    CONSTRAINT SPRING_SESSION_ATTRIBUTES_FK FOREIGN KEY (SESSION_PRIMARY_ID) REFERENCES SPRING_SESSION(PRIMARY_ID) ON DELETE CASCADE
) ENGINE=INNODB ROW_FORMAT=DYNAMIC;

2. Table x doesn’t exist

一般报出这种问题PreparedStatementCallback:bad SQL grammar [DELETE FROM xxx WHERE ?]...Table xxx doesn't exist的原因是,项目所涉及到的数据库中没有当前表,如下所示:

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [select * from Dynamic_Task where timer_Task_Status is null or timer_Task_Status = ? ]; nested exception is java.sql.SQLSyntaxErrorException: Table 'jqp.dynamic_task' doesn't exist
	at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1541) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:667) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:713) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:744) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:757) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at org.springframework.jdbc.core.JdbcTemplate.query(JdbcTemplate.java:815) ~[spring-jdbc-5.3.14.jar:5.3.14]
	at com.jqp.admin.db.service.impl.MysqlJdbcDaoImpl.find(MysqlJdbcDaoImpl.java:109) ~[classes/:na]

直接在项目中创建当前缺失的表即可。

3. You have an error in your SQL syntax;

nested exception is java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax

这种情况的错误一般是,我们在使用orm框架,比如mybatis框架、hibernate框架。我们在编写映射代码时,由于标点符号不正确,或者其他编写不正确,造成生成的mysql语句,出现了mysql语法的错误,如下代码所示:

SELECT
   DATE_FORMAT( pay_order.pay_time, '%Y%m%d' ) AS pay_time,
   pay_order.pay_type_code AS pay_type_code,
   park.NAME AS park_name
FROM
   `pay_order`
LEFT JOIN park_order ON pay_order.trade_pay_no = park_order.id
LEFT JOIN park ON park_order.park_id = park.id
WHERE
   pay_order.STATUS = 2
   <if test="trandate !=null and trandate!=''">and DATE_FORMAT(pay_order.pay_time,'%Y%m%d') = #{trandate}</if>
   <if test="parkName !=null and parkName!=''">and park.name = #{parkName}</if>
   <if test="payType !=null and payType!=''">and pay_order.pay_type_code = #{payType};</if>
GROUP BY
   pay_time,
   pay_type_code,
   park_name

上述mysqlNavicat中可以执行,后来才发现在group by关键字前面,也就是最后一个if的后面,多加了一个逗号,

此时,我们需要检查我们的映射代码,即一定要仔细检查mysql

4. Unknown column x in x

如下问题所述


java.sql.SQLSyntaxErrorException: Unknown column ‘lastname’ in ‘field list’; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: Unknown column ‘lastname’ in ‘field list’
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:239)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:70)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:88)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:440)
at com.sun.proxy.$Proxy54.selectOne(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectOne(SqlSessionTemplate.java:159)
at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:90)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
at com.sun.proxy.$Proxy60.selectById(Unknown Source)
at cn.yswu.test.select(test.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

这种错误一般是数据库中的字段与Java程序实体pojo或者entity类中定义的字段不匹配。

观察发现mysql中为last_name字段,经过驼峰转换可为lastName,但pojo类中则定义了lastname,改为lastName即可。

5. Unknown column ‘xxx’ in ‘where clause’

如下代码所示:

@Override
public List<Enterprise> getUserEnterpriseList(User user) {
  List<Enterprise> list = jdbcService.find("select * from enterprise where id in(" +
               "select enterprise_id id from enterprise_manager " +
               "where user_id = ? " +
               "union all " +
               "select enterprise_id from enterprise_user " +
               "where user_id = ? ) ",Enterprise.class,user.getId(),user.getId());
   return list;
}

报出的错误是:

在这里插入图片描述
错误原因是我在enterprise 表中未创建user_id 字段。

一般这种错误就是没有在指定表中创建对应的字段,在表中创建该字段即可。

好博客就要一起分享哦!分享海报

此处可发布评论

评论(0展开评论

暂无评论,快来写一下吧

展开评论

您可能感兴趣的博客

客服QQ 1913284695