insert if not exist from arraylist<vector> - java

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.

Related

MySQL Syntax Issue when using Accounts.UUID AND UUID =, with two tables

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?)

select from one table to another table in jdbc

How to execute SQL Server query into JDBC like this SQL?
select * into bk_table from existing_table
Expected: Above query should be executed through JDBC.
Actual: Query is not working through JDBC in any way.I tried following two ways but nothing happens or affected in DBMS.
String sql="select * into bk_table from existing_table";
//tried way 1-> result: false
statement.execute(sql);
//tried way 2-> result: -1
statement.executeUpdate(sql);
What to do now?
You need to use an insert-select statement. It can be called with executeUpdate:
String sql = "INSERT INTO bk_table SELECT * FROM existing_table";
statement.executeUpdate(sql);
To select data from old table, "SELECT * FROM bk_table";
To insert the selected data from old to existing table, "INSERT INTO existing_table SELECT * FROM bk_table";

Delete from table on same select same table mariadb using jpa

I need delete from table on operation of same table .JPA query is
DELETE FROM com.model.ElectricityLedgerEntity a
Where a.elLedgerid IN
(SELECT P.elLedgerid FROM
(SELECT MAX(b.elLedgerid)
FROM com.model.ElectricityLedgerEntity b
WHERE b.accountId='24' and b.ledgerType='Electricity Ledger' and b.postType='ARREARS') P );
I got this error:
with root cause org.hibernate.hql.ast.QuerySyntaxException: unexpected
token: ( near line 1, column 109 [DELETE FROM
com.bcits.bfm.model.ElectricityLedgerEntity a Where a.elLedgerid IN (
SELECT P.elLedgerid FROM ( SELECT MAX(b.elLedgerid) FROM
com.bcits.ElectricityLedgerEntity b WHERE b.accountId='24'
and b.ledgerType='Electricity Ledger' and b.postType='ARREARS') P ) ]
at
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)
at
org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)
at
org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82)
at
org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:284)
Same query is running on mysql terminal ,but this is not working with jpa .Can any one tell me how i can write this query using jpa .
I don't understand why do you use Pbefore the last parenthesis...
The following code is not enough ?
DELETE FROM com.model.ElectricityLedgerEntity a
Where a.elLedgerid IN
(SELECT MAX(b.elLedgerid)
FROM com.model.ElectricityLedgerEntity b
WHERE b.accountId='24' and b.ledgerType='Electricity Ledger' and
b.postType='ARREARS')
Edit for bypassing mysql subquery limitations :
The new error java.sql.SQLException: You can't specify target table 'LEDGER' for update in FROM clause
is known in mysql when you use it with JPA. It's one MySQL limitation.
A recent stackoverflow question about it
In brief, you cannot "directly" updated/deleted a table that you query in a select clause
Now I understand why your original query did multiple subqueries seemingly not necessary (while it was useful for mysql) and had a "special" syntax.
I don't know tricks to solve this problem in JPA (I don't use the MySQL DBMS for a long time now).
At your place, I would do two queries. The first where you select the expected max elLedgerid and the second where you could delete line(s) with the id retrieved in the previous query.
You should not have performance issues if your sql model is well designed, the sql indexes well placed and the time to access to the database is correct.
You cannot do this in a single query with Hibernate. If you want to delete the max row(s) with Hibernate you will have to do so in two steps. First, you can find the max entry, then you can delete using that value in the WHERE clause.
But the query you wrote should actually run as a raw MySQL query. So why don't you try executing that query as a raw query:
String sql = "DELETE FROM com.model.ElectricityLedgerEntity a " +
"WHERE a.elLedgerid IN (SELECT P.elLedgerid FROM " +
"(SELECT MAX(b.elLedgerid) FROM com.model.ElectricityLedgerEntity b " +
"WHERE b.accountId = :account_id AND b.ledgerType = :ledger_type AND " +
" b.postType = :post_type) P );";
Query query = session.createSQLQuery(sql);
query.setParameter("account_id", "24");
query.setParameter("ledger_type", "Electricity Ledger");
query.setParameter("post_type", "ARREARS");
Just want to extend existing answer:
In brief, you cannot "directly" updated/deleted a table that you query in a select clause
This was lifted with starting from MariaDB 10.3.1:
Same Source and Target Table
Until MariaDB 10.3.1, deleting from a table with the same source and target was not possible. From MariaDB 10.3.1, this is now possible. For example:
DELETE FROM t1 WHERE c1 IN (SELECT b.c1 FROM t1 b WHERE b.c2=0);

SQL Firebird implementation in java/ IBSQL

so tried to put that SQL code into my java-aplication:
SELECT DISTINCT
StRzImRo.Rohstoff, StRo.Bezeichnung,
CAST (SUM(BwLsImAt.Lieferungen * StRzImRo.Menge * StAt.PROD__REZEPTURGEWICHT / Coalesce(StRz.PARM__BEZUGSGROESSE,1)) AS NUMERIC (9,3)) Rohstoffverbrauch_Gesamt FROM BwLsImAt
JOIN StAt ON (StAt.IntRowId = BwLsImAt.Artikel)
JOIN StRz ON (StRz.IntRowId = StAt.PROD__REZEPTUR)
JOIN StRzImRo ON (StRzImRo.Master = StRz.IntRowId)
JOIN StRo ON (StRzImRo.Rohstoff = StRo.IntRowId)
WHERE StAt.IntRowId > 0
GROUP BY StRzImRo.Rohstoff, StRo.Bezeichnung
-- GROUP BY StRzImRo.Rohstoff, StRzImRo.Menge, StAt.PROD__REZEPTURGEWICHT, Coalesce(StRz.PARM__BEZUGSGROESSE,1)
The code is fully funcional and tested in IBSQL but not working in my java-application.
My app does work properly with other code. I get this error:
org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544569. Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, column 266
ON
I would be very happy if someone could help me with this problem. Thanks!
P.S.: Sorry for my bad language, but i´m not a native speaker
The error suggests there is an ON in an unexpected place in your query, and as the query itself looks fine, my guess is the problem is with the way you construct the query in your Java application. There might be some whitespace missing in your query.
My guess is that you have something like
query = "SELECT * " +
"FROM table1" +
"JOIN table2 ON " //.....
The missing whitespace will make the SQL:
SELECT * FROM table1JOIN table2 ON ....
For the parser, this is perfectly valid until it encounters the ON token, which triggers the error. Eg the parser identifies it is a SELECT with * (all) columns from table1JOIN with alias table2. During parsing the server doesn't check if the table actually exists, so it doesn't trip over the fact that table1JOIN doesn't exist. That is checked after parsing is successfully completed.

Insert from array<> java into table sql where not exists [duplicate]

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.

Categories