I am trying to develop a spring boot application with my sql as the database. i have used jdbc . when trying to add multiple sql statements in the create_table.sql file it gives me an error which leads to cyclic bean errors . but if i added only one sql statement it wont give any errors. it also gave me this bean error even if i have added only one statement if there was not a table present in the db that matches it.
here i have provided the method in the dao file that starts the error.
@PostConstruct
public void createTables() {
try (InputStream script = new ClassPathResource("create_table.sql").getInputStream()) {
jdbcTemplate.execute(new String(script.readAllBytes()));
} catch (IOException e) {
throw new RuntimeException("Failed to execute create_table.sql script", e);
}
}
here is the error part corresponding to sql.
Caused by: org.springframework.jdbc.BadSqlGrammarException: StatementCallback; bad SQL grammar [CREATE TABLE IF NOT EXISTS counter_data (
id bigint NOT NULL AUTO_INCREMENT,
timeStamp varchar(150) NOT NULL,
counter_1 int,
counter_2 int,
counter_3 int,
counter_4 int,
counter_5 int,
counter_6 int,
counter_7 int,
counter_8 int,
PRIMARY KEY (id)
);
--
-- CREATE TABLE IF NOT EXISTS molding_data (
-- id bigint NOT NULL AUTO_INCREMENT,
-- timeStamp varchar(150) NOT NULL,
-- counter_1 int,
-- counter_2 int,
-- counter_3 int,
-- counter_4 int,
-- counter_5 int,
-- counter_6 int,
-- counter_7 int,
-- counter_8 int,
-- PRIMARY KEY (id)
-- );
CREATE TABLE IF NOT EXISTS production_data (
id bigint NOT NULL AUTO_INCREMENT,
timeStamp varchar(150) NOT NULL,
total_production int,
PRIMARY KEY (id)
);
-- CREATE TABLE IF NOT EXISTS shift_data (
-- id bigint NOT NULL AUTO_INCREMENT,
-- timeStamp varchar(150) NOT NULL,
-- shift_production int,
-- PRIMARY KEY (id)
-- );]
at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:103) ~[spring-jdbc-6.1.2.jar:6.1.2]
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:107) ~[spring-jdbc-6.1.2.jar:6.1.2]
at org.springframework.jdbc.core.JdbcTemplate.translateException(JdbcTemplate.java:1548) ~[spring-jdbc-6.1.2.jar:6.1.2]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:408) ~[spring-jdbc-6.1.2.jar:6.1.2]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:444) ~[spring-jdbc-6.1.2.jar:6.1.2]
at io.xdoto.dao.impl.DataDaoImpl.createTables(DataDaoImpl.java:34) ~[classes/:na]
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMethod.invoke(InitDestroyAnnotationBeanPostProcessor.java:457) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:401) ~[spring-beans-6.1.2.jar:6.1.2]
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:219) ~[spring-beans-6.1.2.jar:6.1.2]
... 46 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: 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 'CREATE TABLE IF NOT EXISTS production_data (
id bigint NOT NULL AUTO_INCRE' at line 29
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:121) ~[mysql-connector-j-8.1.0.jar:8.1.0]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.1.0.jar:8.1.0]
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:763) ~[mysql-connector-j-8.1.0.jar:8.1.0]
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:648) ~[mysql-connector-j-8.1.0.jar:8.1.0]
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:94) ~[HikariCP-5.0.1.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-5.0.1.jar:na]
at org.springframework.jdbc.core.JdbcTemplate$1ExecuteStatementCallback.doInStatement(JdbcTemplate.java:435) ~[spring-jdbc-6.1.2.jar:6.1.2]
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:393) ~[spring-jdbc-6.1.2.jar:6.1.2]
... 53 common frames omitted
i was trying to create my sql tables that is needed in the application, if they dont exist .
timeStamp is a keyword,so switch to something else.