Syntax error at or near "$1" jdbc postgres -> SET var=? - java

Trying to execute the following code :
this.getSessionFactory().getCurrentSession()
.createNativeQuery("SET app.variable = :variable")
.setParameter("variable","variableValue")
.executeUpdate();
It is giving me the following error :
org.postgresql.util.PSQLException: ERROR: syntax error at or near "$1"
Position: 26
I'm not sure if this is a bug in JDBC or PostgreSQL.
It seems not a PostgreSQL issue as when I use the DBeaver using bind variables the query works fine.
Tried the same query by creating a prepared statement as well and the Result was same.

The SQL statement SET accepts no parameters. Only SELECT, VALUES, INSERT, UPDATE, DELETE and MERGE do.

Related

"Invalid Column Name" thrown In Java Spring Batch, but not in Oracle Sql Developer

Using Java Spring Batch, I am generating a file based off of a query sent to a Oracle database:
SELECT
REPLACE(CLIENT.FirstName, chr(13), ' ')
FROM client_table CLIENT;
This query works fine when I run it in Oracle SQL Developer when I spool the result, but doesn't work when I try to utilize it to generate a file in Java Spring batch. It throws the error:
Message=Encountered an error executing step preparePrimaryIpData in job extract-primary-ip-job
org.springframework.jdbc.BadSqlGrammarException: Attempt to process next row failed; bad SQL grammar [
SELECT
REPLACE(CLIENT.FirstName, chr(13), ' ')
FROM client_table CLIENT
]; nested exception is java.sql.SQLException: Invalid column name
at org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator.doTranslate(SQLErrorCodeSQLExceptionTranslator.java:237)
Why is this working fine in Oracle Sql Developer but not when I try to utilize it in Java Spring Batch?
Also, the alias is necessary, because my actual query has a lot more joins, I just wanted to simplify it as an example.
you need to check the spelling of columns in your query as well as where you will be getting the result set. I was facing the same problem and the problem was in spelling in one of the "rs.getString("COLUMN_NAME")".
It is not the sql that has failed but you are misspelling the column name somewhere in your code.
Try some thing like :
SELECT REPLACE(CLIENT.FirstName, chr(13), ' ') columnNameX FROM client_table CLIENT

SQL Query works in workbench but gives syntax error in java

I ran the query in both sql Workbench and in the executeUpdate() method in java:
in Workbench:
INSERT INTO recentsearches (name) VALUES ("blah");
in java:
statement.executeUpdate("INSERT INTO recentsearches (name) VALUES (\""+name+"\""));
Assuming name = "blah". But I get a syntax error from running the query in java, I've already checked the string value for name. It definitely comes up as "blah", and I didn't forget the speech marks around string values, yet I still get a syntax error.
The error I get in my console is:
check the manual that corresponds to your MySQL server version for the
right syntax to use near '' at line 1
Try to use:
"INSERT INTO recentsearches (name) VALUES("+name+")";
My advice, use PreparedStatement because it has:
-Precompilation and DB-side caching of the SQL statement leads to overall faster execution and the ability to reuse the same SQL statement in batches.
-Automatic prevention of SQL injection attacks by builtin escaping of quotes and other special characters. Note that this requires that you use any of the PreparedStatement setXxx() methods to set the values

Hibernate exception Query not properly ended

Getting exception while running this query
select COUNT(tl.LOG_ID)AS EVTCOUNT,tl.PRIORITY FROM Customer_? tl Where tl.DEVICE_REPORTED_TIME >= SysDate-90 GROUP BY tl.PRIORITY ORDER BY tl.PRIORITY`
Here I am using query.setLong(0,custId);
so it will become like Customer_1
If I run the above query from SqlDeveloper IDE It is working fine or If I set this value as Statically like Customer_1 instead of Customer_?
It working fine.
Errors:
WARN : org.hibernate.util.JDBCExceptionReporter - SQL Error: 933, SQLState: 42000
ERROR: org.hibernate.util.JDBCExceptionReporter - ORA-00933: SQL command not properly ended
What Might be wrong with this query though other queries are running fine?
Edit
I am using NamedQueries and I have written this query in a separate xml file.
You can't use a parameter in a query like that. Without going into the logic behind this, your best option is to concatenate the query string before creating a query
String queryString = "select COUNT(tl.LOG_ID) AS EVTCOUNT,tl.PRIORITY FROM Customer_" + custId + " tl Where tl.DEVICE_REPORTED_TIME >= SysDate-90 GROUP BY tl.PRIORITY ORDER BY tl.PRIORITY";
Query query = session.createSQLQuery(queryString);
EDIT
As for NamedQueries, they are compiled when the application is started so I don't think there is a way to change the target entity (table) at runtime.

java.sql.SQLException: Incorrect syntax near the keyword 'top'

I have stored procedure
CREATE PROC dbo.TestSproc
as
select COUNT(*) from dbo.TestCase
It doesn't make any useful things but I cannot invoke it via hibernate
Query query = m_entityManager.createNativeQuery("CALL TestSproc()");
query.getSingleResult();
it causes java.sql.SQLException: Incorrect syntax near the keyword 'top'. other procedures has the same problem.
and there is no TOP statement in this procedures. Has anyone any ideas how it could be fixed?
If you're using SQL Server, you might want to try executing the sproc with exec TestSproc instead of CALL TestSproc(). I would guess the TOP error is from getSingleResult(). Fix the sproc error first.

Java + MySQL - Syntax error but the statement is correct

My program executes INSERT query. When I run it, I get an error
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 'INSERT INTO words(lang1, lang2, category, lang2_code, user) SELECT 'ahoj', 'hell' at line 1
I tried to print the actual statement to the stdout:
SET #lang:='Angličtina', #categ:='Nová';
INSERT INTO words(lang1, lang2, category, lang2_code, user)
SELECT 'ahoj', 'hello', c.id, l.id, 1 FROM categories c, languages l
WHERE c.name = #categ AND l.name = #lang;
As you can see, the statement is altered in the log. 'hell' instead of 'hello'. When I copy that into the mysql command line and execute, it works just fine so I assume the problem is in the JDBC somewhere.
That's not one statement. If you want to use more than one statement at a time, don't use a PreparedStatement but for example addBatch but it seems that here you should simply issue 2 JDBC statements, one after the other.

Categories