how to resolve org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; - sql

I am using HSQLDB as my database. i want to get a primary key of latest inserted row. for that i have return a query in my java class as below:
final String query = "INSERT INTO polling_log (start_date,status,action) VALUES(CURRENT_TIMESTAMP,?,?); CALL IDENTITY();";
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
int update = adapterJdbcTemplate.update(new PreparedStatementCreator() {
#Override
public PreparedStatement createPreparedStatement(
Connection connection) throws SQLException {
PreparedStatement preparedStatement = connection
.prepareStatement(query);
preparedStatement.setInt(1, pollingLogVO.getStatus());
preparedStatement.setString(2, pollingLogVO.getAction());
System.out.println(preparedStatement.getGeneratedKeys().getFetchSize());
return preparedStatement;
}
}, generatedKeyHolder);
System.out.println("###################### "+ update);
Number logId = generatedKeyHolder.getKey();
pollingLogId = logId.intValue();
and to store the query i have used GeneratedKeyHolder. but while runing this i get an exception:
org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar []; nested exception is java.sql.SQLException: unexpected token: IDENTITY
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:98)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:602)
at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:817)
at com.platysgroup.lmex.adapter.moodle.dao.LogDao.insertPollingLog(LogDao.java:36)
at com.platysgroup.lmex.adapter.MoodlePostingTask.insertPollingLog(MoodlePostingTask.java:134)
at com.platysgroup.lmex.adapter.MoodlePostingTask.run(MoodlePostingTask.java:55)
at java.util.TimerThread.mainLoop(Timer.java:512)
at java.util.TimerThread.run(Timer.java:462)
Caused by: java.sql.SQLException: unexpected token: IDENTITY
at org.hsqldb.jdbc.Util.sqlException(Unknown Source)
at org.hsqldb.jdbc.JDBCPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.JDBCConnection.prepareStatement(Unknown Source)
at org.apache.commons.dbcp.DelegatingConnection.prepareStatement(DelegatingConnection.java:248)
at org.apache.commons.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.prepareStatement(PoolingDataSource.java:302)
at com.platysgroup.lmex.adapter.moodle.dao.LogDao$1.createPreparedStatement(LogDao.java:41)
at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:580)
... 6 more
Caused by: org.hsqldb.HsqlException: unexpected token: IDENTITY
at org.hsqldb.error.Error.parseError(Unknown Source)
at org.hsqldb.ParserBase.unexpectedToken(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
... 12 more

The problem is with this line (wrapped for clarity):
final String query = "INSERT INTO polling_log (start_date,status,action) VALUES(CURRENT_TIMESTAMP,?,?); CALL IDENTITY();";
The issue is that IDENTITY is a reserved word in SQL; it's got a meaning pre-defined already and so can't be used like that in a CALL statement. (I don't know what it is actually used for; the full definition of SQL is huge and has a very large number of reserved words.) The immediate work around would be to enclose the problem word in double quotes (which would need to be backslash-quoted because of being in a Java string):
final String query = "INSERT INTO polling_log (start_date,status,action) VALUES(CURRENT_TIMESTAMP,?,?); CALL \"IDENTITY\"();";
However, if you're just calling that to get the inserted row, STOP! Just let Spring do the work for you, assuming you have JDBC 3.0 or later (i.e., Java 5 or later).

As far as I know you cannot put more than one statements into one string to be executed. Execute two separate operations instead of this.

The following link has what you need:
http://www.devdaily.com/blog/post/jdbc/spring-jdbc-insert-auto-generated-key

Related

JDBC | SQL Anywhere Error -188: Not enough values for host variables

JDBC or Hibernate is showing strange behavior while calling a stored procedure.
Here is the sample code:
return session.createSQLQuery("select * from pr_ss_coo_10x('31748','NEWRX',null,'5053')")
..
....tolist();
It is throwing error
Caused by: org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:106)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:42)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:109)
at org.hibernate.loader.Loader.doList(Loader.java:2620)
at org.hibernate.loader.Loader.doList(Loader.java:2600)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2429)
at org.hibernate.loader.Loader.list(Loader.java:2424)
at org.hibernate.loader.custom.CustomLoader.list(CustomLoader.java:336)
at org.hibernate.internal.SessionImpl.listCustomQuery(SessionImpl.java:1967)
at org.hibernate.internal.AbstractSessionImpl.list(AbstractSessionImpl.java:322)
at org.hibernate.internal.SQLQueryImpl.list(SQLQueryImpl.java:125)
at com.mps.surescript.repository.impl.SSGetInfoRepoImpl.getBenefitCoordInformation(SSGetInfoRepoImpl.java:79)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:333)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 76 common frames omitted
Caused by: com.sybase.jdbc3.jdbc.SybSQLException: SQL Anywhere Error -188: Not enough values for host variables
at com.sybase.jdbc3.tds.Tds.a(Unknown Source)
at com.sybase.jdbc3.tds.Tds.nextResult(Unknown Source)
However, it is working fine when I fire the same query through SQL Anywhere client. Sybase error code document says
"You have not provided enough host variables for either the number of
bind variables, or the statement, or the number of SELECT list items."
I have checked it twice and the result set is correctly mapped with the Pojo. What can be the possible root cause for this problem?
Bypassing the Hibernate transaction Management did the trick and fetching result set using PreparedStatement.
Connection con = DriverManager.getConnection(url, user, password);
PreparedStatement ps = con.prepareStatement(
"select * from pr_ss_coo_10x(#param1=?,#param2=?, #param3=?, #param4=? )");
ResultSet rs = ps.executeQuery();
This is not a solution, it is just a way around.

Junit HSQLDB - user lacks privilege or object not found - THIS_.oh-ordnbr

I am getting an exception when my column name contains hyphen "-"
Entity : this is the entity name.
#Entity
#Table(name = "RequestHeader")
public class RequestHeader implements Serializable {
....
....
#Column(name = "`oh-ordnbr`")
private Integer ohOrdnbr;
Schema definition: This is the query for schema creation.
CREATE MEMORY TABLE PUB.REQUESTHEADER(
REQUESTID INTEGER,
IMUSERID INTEGER,
REQUESTDATE DATE,
REQUESTTIME INTEGER,
REQUESTSTATUS VARCHAR(19),
REQUESTTYPE VARCHAR(22),
HEADERINSTRUCTIONS VARCHAR(5150),
DATEFORMAT VARCHAR(20),
TIMEFORMAT VARCHAR(20),
LANGUAGEID INTEGER,
"OH-ORDNBR" INTEGER,
"OH-TRCNSTAMP" INTEGER,
ISPICKUPLIST BIT(1),
CONSTRAINT "RQH-1" PRIMARY KEY(REQUESTID)
);
The error is below:
Exception Stack: Error message which I have received by running the Junit.
Caused by: org.hsqldb.HsqlException: user lacks privilege or object not found: THIS_.oh-ordnbr
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.error.Error.error(Unknown Source)
at org.hsqldb.ExpressionColumn.checkColumnsResolved(Unknown Source)
at org.hsqldb.QueryExpression.resolve(Unknown Source)
at org.hsqldb.ParserDQL.compileCursorSpecification(Unknown Source)
at org.hsqldb.ParserCommand.compilePart(Unknown Source)
at org.hsqldb.ParserCommand.compileStatement(Unknown Source)
at org.hsqldb.Session.compileStatement(Unknown Source)
at org.hsqldb.StatementManager.compile(Unknown Source)
at org.hsqldb.Session.execute(Unknown Source)
Could some one help me in fixing this?
The reason for the object not found error is the fact that the oh-ordnbr column is defined to be case sensitive (this is due to the double quotes you put around it).
You have two possible solutions:
Do not use dashes (hyphens) in your SQL. It is bad practice anyway, use underscores instead.
Update the JPA annotation as follows:
#Column(name = "`OH-ORDNBR`")
private Integer ohOrdnbr;
I strongly recommend using underscores instead of dashes, you never know what weirdness different JPA implementations might have when using the second solution.
Check your configuration file has correct provider, I fixed same issue by providing org.hibernate.ejb.HibernatePersistence provider.
If HSQL Scripts are run via Java and if any new table query is followed by select/alter/update queries it always throws "user lacks privilege object". In reality the create queries after execution with Java Statements never gets commited into the DB and it is not persisted and we run our select/alter/update and it results in the exception. If we run the queries line by line and commit for each line, we could avoid this error.

SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported using Groovy

I am trying to run a query using Groovy SQL against a SQL Server DB. However, I am receiving the following exception: com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported
Grails version: 2.2.5
SQL Server version: 9.0
The script I am running from the Grails console:
import groovy.sql.*
Sql sql = Sql.newInstance(ctx.dataSource_sqlserver)
def query = '''
select
avg(cast(score as decimal)) average_score
from
StudentQuestionScores
where
id in ( :ids )
and question = :question
'''
def ids = Student.list().collect { it.id } // Succeeds
def row = sql.firstRow(query, [ids: ids, question: 'Insert Question Here']) // ERROR
sql.close()
println row.average_score // Never makes it here :(
Does anyone know why this might be or how I can fix it? Thank you!
Stacktrace:
com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.DataTypes.throwConversionError(DataTypes.java:1117)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObject(SQLServerPreparedStatement.java:991)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObjectNoType(SQLServerPreparedStatement.java:926)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.setObject(SQLServerPreparedStatement.java:935)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:169)
at org.apache.commons.dbcp.DelegatingPreparedStatement.setObject(DelegatingPreparedStatement.java:169)
at groovy.sql.Sql.setObject(Sql.java:3649)
at groovy.sql.Sql.setParameters(Sql.java:3614)
at groovy.sql.Sql.getPreparedStatement(Sql.java:3875)
at groovy.sql.Sql.getPreparedStatement(Sql.java:3922)
at groovy.sql.Sql.access$900(Sql.java:227)
at groovy.sql.Sql$PreparedQueryCommand.runQuery(Sql.java:4078)
at groovy.sql.Sql$AbstractQueryCommand.execute(Sql.java:4027)
at groovy.sql.Sql.rows(Sql.java:1945)
at groovy.sql.Sql.rows(Sql.java:1835)
at groovy.sql.Sql.rows(Sql.java:1816)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

soapUI: How to access Test Step property from assertion script?

I'm new to SoapUI and Groovy, but an experienced Java programmer.
I created a TestCase with two Test Steps:
Properties step called CID with single property correlationID and value ${=java.util.UUID.randomUUID()}.
Test Request where I put <CorrelationID>${correlationID}</CorrelationID> in the request.
It works perfectly and submits unique CorrelationID value every time I run my tests.
Now I want to add new Script Assertion to Test Step 2 (Test Request) that compares computed correlationID property value from Test Step 1 (CID) with some data from Test Step 2 response. The problem is that I can not seem to be able to access generated value of correlationID from there.
If I try this: log.info "${correlationId}"
I get: No such property: correlationId for class: Script19
If I try this: log.info "${CID#correlationId}"
I get:
startup failed:
Script43.groovy: 1: unexpected char: '#' # line 1, column 16.
log.info "${CID#correlationId}"
^
org.codehaus.groovy.syntax.SyntaxException: unexpected char: '#' # line 1, column 16.
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:97)
at org.codehaus.groovy.antlr.AntlrParserPlugin.parseCST(AntlrParserPlugin.java:71)
at org.codehaus.groovy.control.SourceUnit.parse(SourceUnit.java:236)
at org.codehaus.groovy.control.CompilationUnit$1.call(CompilationUnit.java:158)
at org.codehaus.groovy.control.CompilationUnit.applyToSourceUnits(CompilationUnit.java:814)
at org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:511)
at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:487)
at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:464)
at groovy.lang.GroovyClassLoader.doParseClass(GroovyClassLoader.java:306)
at groovy.lang.GroovyClassLoader.parseClass(GroovyClassLoader.java:287)
at groovy.lang.GroovyShell.parseClass(GroovyShell.java:727)
at groovy.lang.GroovyShell.parse(GroovyShell.java:739)
at groovy.lang.GroovyShell.parse(GroovyShell.java:766)
at groovy.lang.GroovyShell.parse(GroovyShell.java:757)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.compile(SoapUIGroovyScriptEngine.java:148)
at com.eviware.soapui.support.scripting.groovy.SoapUIGroovyScriptEngine.run(SoapUIGroovyScriptEngine.java:93)
at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.assertScript(GroovyScriptAssertion.java:116)
at com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.GroovyScriptAssertion.internalAssertResponse(GroovyScriptAssertion.java:133)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlMessageAssertion.assertResponse(WsdlMessageAssertion.java:156)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.assertResponse(WsdlTestRequest.java:189)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequest.setResponse(WsdlTestRequest.java:159)
at com.eviware.soapui.impl.wsdl.teststeps.WsdlTestRequestStep.run(WsdlTestRequestStep.java:346)
at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.runTestStep(WsdlTestCaseRunner.java:207)
at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.internalRun(WsdlTestCaseRunner.java:138)
at com.eviware.soapui.impl.wsdl.testcase.WsdlTestCaseRunner.internalRun(WsdlTestCaseRunner.java:39)
at com.eviware.soapui.impl.wsdl.support.AbstractTestRunner.run(AbstractTestRunner.java:135)
at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: Script43.groovy:1:16: unexpected char: '#'
at org.codehaus.groovy.antlr.parser.GroovyLexer.nextToken(GroovyLexer.java:695)
at org.codehaus.groovy.antlr.parser.GroovyLexer$1.nextToken(GroovyLexer.java:248)
at groovyjarjarantlr.TokenBuffer.fill(TokenBuffer.java:69)
at groovyjarjarantlr.TokenBuffer.LA(TokenBuffer.java:80)
at groovyjarjarantlr.LLkParser.LA(LLkParser.java:52)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.nls(GroovyRecognizer.java:780)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.openOrClosableBlock(GroovyRecognizer.java:8848)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.stringConstructorValuePart(GroovyRecognizer.java:13449)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.stringConstructorExpression(GroovyRecognizer.java:11932)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.primaryExpression(GroovyRecognizer.java:11091)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.argumentLabel(GroovyRecognizer.java:10863)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.commandArgument(GroovyRecognizer.java:10756)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.commandArguments(GroovyRecognizer.java:10173)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.expressionStatement(GroovyRecognizer.java:8948)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.statement(GroovyRecognizer.java:1258)
at org.codehaus.groovy.antlr.parser.GroovyRecognizer.compilationUnit(GroovyRecognizer.java:650)
at org.codehaus.groovy.antlr.AntlrParserPlugin.transformCSTIntoAST(AntlrParserPlugin.java:93)
... 31 more
1 error
How can I access that correlationId computed value from my assertion Groovy script?
Thank you
The problem with my approach was that even if I do access the property, it is dynamic and my UUID is different each time I read the value of the property.
Instead I replaced my Properties test step with Setup Script of the Test Case:
uuid = context.expand('${=java.util.UUID.randomUUID()}')
testRunner.testCase.setPropertyValue("correlationID", uuid)
Each time my test case executes, a new uuid is generated. Then future test steps may access this static test case level property.
In my Test Request I use:
<CorrelationID>${#TestCase#correlationID}</CorrelationID>
In script assertion I use:
correlationID = context.expand('${#TestCase#correlationID}')
Thanks to Unhandled from SoapUI forums.
Wanted to add that context.getPropertyNames() or context.getProperties() does not list the properties defined in a 'Property' Test step.
Only context.expand('') works.
To get the property names of current Test Step from Script Assertion, we can use the following codes -
def testStepProp = context.getCurrentStep().getPropertyNames()
for(i=0; i<testStepProp.size(); i++){
log.info testStepProp[i]
}
To get the property names of current Test Case from Script Assertion, we can use the following codes:
def testCaseProp = context.testCase.getPropertyNames()
for(i=0; i<testCaseProp .size(); i++){
log.info testCaseProp[i]
}
To get a property value from script Assertion:
For Example if my property name is 'correlationID' -
def myValue = context.testCase.getPropertyValue("correlationID") //If we are in same testCase
def myValue = context.testCase.testSuite.testCases["testCaseName"].getPropertyValue("correlationID") //If we are in different testCase
Please note that 'testRunner' doesn't work in script Assertion. Instead we need to use context.

Failing jdbc INSERT INTO statement to MS Access database

statement.executeUpdate("INSERT INTO countrylookup (Country, DialCode) VALUES('Iran', '957')")
Running this statement gives me no error output in the console, but when I check the database no update/insert is made. What could be the reason for this?
The access to the database itself is successful, and fetching values with a statement such as SELECT * FROM countrylookup succeeds.
I tried the preparedStatement approach aswell with the exact same result. The file is not open when I execute the command.
UPDATE: Stacktrace: (first row in Swedish means "INSERT INTO-expression contains the following unknown fieldname: 'Pa_RaM000'. Please check that the name is rightly spelled and try again.)
Exception in thread "main" java.sql.SQLException: [Microsoft][Drivrutin f?r ODBC Microsoft Access] INSERT INTO-uttrycket inneh?ller f?ljande ok?nda f?ltnamn: 'Pa_RaM000'. Kontrollera att namnet ?r r?ttstavat och f?rs?k igen.
at sun.jdbc.odbc.JdbcOdbc.createSQLException(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.standardError(Unknown Source)
at sun.jdbc.odbc.JdbcOdbc.SQLExecute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(Unknown Source)
at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(Unknown Source)
at MDBAccessor.insertValueIntoField(MDBAccessor.java:43)
at TestRunner.main(TestRunner.java:28)
Is dialcode numeric? If so, remove the quotes from the value.
VALUES('Iran', 957)
In order for the INSERT INTO statement to actually be reflected in the database you have to call connection.commit() followed by connection.close(). Another similar thread describing this: Java General Error On Insert...???