I'm having issues when using this:
WARN 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 'WHERE Accounts.UUID = WarAccounts.UUIDAND Accounts.UUID = 'c7a00fe7-826d-46da-b4' at line 1
Here is the SQL:
SELECT
*
FROM
Accounts,
WarAccounts
WHERE
Accounts.UUID = WarAccounts.UUID
AND Accounts.UUID = ?
(Using prepared statements)
I'm very confused as to what is wrong with this to cause that issue, I've googled it but cannot find anything since this seems to be the correct way.
Server type: MySQL
Server version: 5.5.58-0ubuntu0.14.04.1 - (Ubuntu)
Wild ass guess:
In C# i would do this:
string query = "SELECT *" +
"FROM MYTABLE" +
"WHERE 1=1"
..to get your error..
Concat the string and you'll see the problem:
"SELECT *FROM MYTABLEWHERE 1=1"
The concatination should be:
string query = "SELECT * " +
"FROM MYTABLE " +
"WHERE 1=1 "
(see the spaces at the end of every line?)
Related
In my application, when a User removes a Message, I need to remove the relationship between the 2 entities while keeping them intact. Hence, I tried to delete rows directly from the relationship table using the following ways:
1.
Query q = em.createNativeQuery("DELETE FROM User_Inbox WHERE User_USERNAME = :username AND Message_ID = :messageID");
q.setParameter("username", username);
q.setParameter("messageID", messageID);
q.executeUpdate();
2.
Query q = em.createNativeQuery("DELETE FROM User_Inbox WHERE User_USERNAME = :username AND Message_ID = :messageID");
q.setParameter("username", "'" + username + "'");
q.setParameter("messageID", "'" + messageID + "'");
q.executeUpdate();
The 1st and 2nd approaches produced the following exception:
Internal Exception: 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 ':username AND
Message_ID = :messageID' at line 1
Error Code: 1064
3.
Query q = em.createNativeQuery("DELETE FROM User_Inbox WHERE User_USERNAME = ':username' AND Message_ID = ':messageID'");
q.setParameter("username", username);
q.setParameter("messageID", messageID);
q.executeUpdate();
The 3rd approach did not produce any exceptions. It went through normally. However, nothing was deleted from the User_Inbox table.
4.
Query q = em.createNativeQuery("DELETE FROM User_Inbox WHERE User_USERNAME = '" + username + "' AND Message_ID = '" + messageID + "'");
q.executeUpdate();
The 4th approach worked perfectly. The query went through smoothly and a record was deleted properly.
The last approach worked but the code doesn't look very neat to me. I'd be very grateful if someone could show me what I did wrong with the 1st three approaches.
UPDATE:
Based on the answer from D. Moore, I've just found out that named parameters cannot be used with native queries. This has been mentioned in this answer by Pascal Thivent.
Named parameters follow the rules for identifiers defined in Section
4.4.1. The use of named parameters applies to the Java Persistence query language, and is not defined for native queries. Only
positional parameter binding may be portably used for native
queries.
I agree that (4.) is certainly not the right way to be doing things. It's tedious and unsafe.
(3.) are working, but not producing the expected results since they have extra quotes.
As to why (1.) isn't working, you would want to look into the SQL that is being generated (I believe the settings are specific to the JPA system you are using). You seem to have the code correct, but the error implies that the parameters are not being substituted.
Do positional parameters do anything different?
Query q = em.createNativeQuery("DELETE FROM User_Inbox WHERE User_USERNAME = ?1 AND Message_ID = ?2");
q.setParameter(1, username);
q.setParameter(2, messageID);
q.executeUpdate();
Good evening
i have problem about a query of insert if not exist i have do it in another query and it worked
but now i have an array of class java Service
so i want test about the id : VLAN
because i execut every time and i want that not insert many time if it exist already on table sql
This is my query but they give me an error about it
for(Service srv:service){
srvDataLst.add(srv.getvlan());
PreparedStatement pst=conn.prepareStatement(
"INSERT INTO tout (VLAN,client,JR,vrf,address) "
+ "VALUES(?,?,?,?,?) SELECT DISTINCT "
+ "'"+srv.getvlan()+"' FROM dual WHERE NOT EXISTS("
+ "SELECT * FROM tout WHERE 'VLAN'='"+srv.getvlan()+"') ");
pst.setInt(1,srv.getvlan());
pst.setString(2,convertNullToEmptyString(srv.getdesc()));
pst.setString(3,convertNullToEmptyString(srv.getjr()));
pst.setString(4,convertNullToEmptyString(srv.getvrf()));
pst.setString(5,convertNullToEmptyString(srv.getaddress()));
pst.executeUpdate();
Thank you for help
The error is:
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
'SELECT DISTINCT '20' FROM dual WHERE NOT EXISTS(SELECT * FROM tout
WHERE 'VLAN'=' at line 1
It's either insert into ... values(...) or insert into ... select ..., you can't use both in the same statement.
Good evening
i have problem about a query of insert if not exist i have do it in another query and it worked
but now i have an array of class java Service
so i want test about the id : VLAN
because i execut every time and i want that not insert many time if it exist already on table sql
This is my query but they give me an error about it
for(Service srv:service){
srvDataLst.add(srv.getvlan());
PreparedStatement pst=conn.prepareStatement(
"INSERT INTO tout (VLAN,client,JR,vrf,address) "
+ "VALUES(?,?,?,?,?) SELECT DISTINCT "
+ "'"+srv.getvlan()+"' FROM dual WHERE NOT EXISTS("
+ "SELECT * FROM tout WHERE 'VLAN'='"+srv.getvlan()+"') ");
pst.setInt(1,srv.getvlan());
pst.setString(2,convertNullToEmptyString(srv.getdesc()));
pst.setString(3,convertNullToEmptyString(srv.getjr()));
pst.setString(4,convertNullToEmptyString(srv.getvrf()));
pst.setString(5,convertNullToEmptyString(srv.getaddress()));
pst.executeUpdate();
Thank you for help
The error is:
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
'SELECT DISTINCT '20' FROM dual WHERE NOT EXISTS(SELECT * FROM tout
WHERE 'VLAN'=' at line 1
It's either insert into ... values(...) or insert into ... select ..., you can't use both in the same statement.
I'm attempting to create a JDBC query with the following statement
String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName +
"where datediff(d,DATECOLUMN2,getdate()) <= 1";
st = conn1.createStatement();
rs = st.executeQuery(query); //receiving error here
I am receiving the following error message
java.sql.SQLException: "d" is not a recognized table hints option. If it is intended as a parameter to a table-valued function or to the CHANGETABLE function, ensure that your database compatibility mode is set to 90.
I'm sure the query isn't recognizing the datediff function for some reason I am not sure why since i was previously using HQL in the same application and retrieving the values.
In an attempt to use an alternative function I used
{fn TIMESTAMPADD( SQL_TSI_DAY, 1, CURRENT_TIMESTAMP)}
but it also failed I later on found that this is only used for Derby Database's
Can someone assist me in using the proper sql function to compare a date with the current date using JDBC
String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "+
"where datediff(day,DATECOLUMN2,getdate()) <= 1";
You have a comma before from. Based on the error messages you are running this against SQL server.
String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "
+" where datediff(d,DATECOLUMN2,getdate()) <= 1";
The comma after the "d" should be a dot:
where datediff(d.DATECOLUMN2,getdate())
--------------- ^ dot here
The posted snippet doesn't have a closing double quote between tableName and +, but I figure that is just a typo. However, in your real code, where precisely is the double quote? Is it directly after tablename, like this
String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName" +
or after the space that follows tablename, like this
String query = "SELECT COLUMN1,DATECOLUMN2 FROM tableName "+
It is very likely the former, because in that case the resulting query would look exactly the way as to cause the error you are getting. Take a look at this:
SELECT COLUMN1,DATECOLUMN2 FROM tableNamewhere datediff(d,DATECOLUMN2,getdate()) <= 1
You can see that where merges with the table name and datediff becomes an alias. What follows is interpreted as table hints. (You can specify table hints without WITH in older versions of SQL Server/older compatibility levels.) Consequently, SQL Server stumbles over d, as that is indeed an incorrect table hint.
I'am trying to use COLLATE statement in a Hibernate SQL query but, it doesn't recognize the statement.
CONSULTA: FROM Articulos WHERE activo=0
and (codigodearticulo like '%CIN EMB%' COLLATE='Modern_Spanish_CI_AI'
or descripcion like '%CIN EMB%' COLLATE='Modern_Spanish_CI_AI'
or descripcionadicional like '%CIN EMB%' COLLATE='Modern_Spanish_CI_AI' )
and codigodelinea in
(select CODIGODELINEA from Lineas where CATAUTOPARTES='1')
And when app compile, Hibernate return this exception :
- line 1:107: unexpected token: COLLATE
- line 1:107: unexpected token: COLLATE
- line 1:107: unexpected token: COLLATE
- Error hibernate: unexpected token: COLLATE near line 1, column 107
I can't find the problem, in MSSQL Server it works fine.
Unfortunately HQL isn't a complete replacement for SQL, and doesn't seem to allow specifying Collation at all.
It can be specified on a Criteria query. See this answer
Instead of having in HQL:
String hql = "SELECT e" +
" FROM EntityJPA e" +
" WHERE e.mycolumn COLLATE 'utf8_bin' = 'VALUE'"
Query query = entityManager.createQuery(hql);
You can use a Native Query like this
String sql = "SELECT t" +
" FROM entity_table t" +
" WHERE t.mycolumn COLLATE 'utf8_bin' = 'VALUE'"
and execute it a a Native Query:
Query query = entityManager.createNativeQuery(sql);
This was the simpler solution by keeping in mind that native query create a dependency on the SQL language of the target database (mySQL in our case).
For us this was still acceptable as the possibility to use a different DB Engine is very low.