How to cancel a statement with derby? - java

I have a very long sql statement, with java derby database, and I should want provide to the user a "cancel" things. But I get :
Caused by: java.sql.SQLException: Feature not implemented: cancel.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
So how can I do something like that with derby ?
Thanks.
Edit 1
There is no next loop in my request ; here is my big sql call:
"DELETE FROM TABLE "
+ "WHERE "
+ "REF_TICKET IN
+ "(SELECT NTICKET FROM OTHER_TABLE WHERE "
+ "REF_OPEN IN
+ (SELECT OPEN FROM AGAIN_ANOTHER_TABLE WHERE "
+ "{fn TIMESTAMPDIFF( SQL_TSI_DAY, TIMECLOSE, CURRENT_DATE)} > 365))");
So it's all in one statement.

It's usually not the ExecuteQuery() call that takes very long, but the ResultSet.next() loop that runs for a long time, fetching all the rows. So just check for the user's cancel request during your row-fetching loop, say, every hundred rows or so.

Related

How to set parameter for mysql event name in JpaRepository?

I want to create MySQL Event programmatically with Query Method (#Query) in spring data. And my code like this:
#Query(value="CREATE EVENT :name ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 30 MINUTE ON COMPLETION NOT PRESERVE ENABLE"
+ " DO BEGIN"
+ " END", nativeQuery=true)
void createEventTestOpen(#Param("name") String name);
But i get error like this:
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 ''my_sample_name' ON SCHEDULE AT CURRENT_TIMESTAMP +
INTERVAL 30 MINUTE ON ' at line 1
How can I make a MySQL event with using the query method in Spring with variable event name?

str_to_date mysql does not run in hsql

My code is performing the following update at a given time in my mysql database:
" UPDATE client_registration " +
" SET registration_date = NOW() " +
" WHERE cycle <= str_to_date(\"" + now + "\",'%d/%m/%Y %H:%i') ";
However I have a unit test that tries to perform this update on the HSQL database and I receive the following error message: user lacks privilege or object not found: STR_TO_DATE.
Some way to execute the condition WHERE cycle_start <= str_to_date(\"" + now + "\",'%d/%m/%Y %H:%i') for the mysql database and the hsql database?
You have to re-write your query for HSQL:
" UPDATE client_registration " +
" SET registration_date = NOW() " +
" WHERE cycle <= current_timestamp";
How to do "select current_timestamp" in hsqldb?
If you want to run the exact same query on both MySQL and HSQLDB, you need to create the STR_TO_DATE function on HSQLDB. You also need to use the single-quote character in your query: str_to_date('" + now + "','%d/%m/%Y %H:%i') (this quoting follows the SQL Standard).
It is easier if you use the SQL Standard format 'YYYY-MM-DD hh:mm:ss' (e.g '2020-07-21 14:30:00') for your 'now' variable and the format string you use with MySQL. In this case the HSQLDB function is simply created as:
CREATE FUNCTION STR_TO_DATE(STR VARCHAR(30), FORMAT VARCHAR(40) )
RETURNS TIMESTAMP
RETURN CAST(STR AS TIMESTAMP);
Execute the CREATE FUNCTION statement once when you connect to the database and you can use it in all your queries and upade statements.

How do I make my function for testdata for my database faster?

I want to create testdata and have written a function for storing the products, my product generators generate in my database.
The plan is to create about 10,000,000 products or more for testing purposes.
I want to check every time before I insert a product, if the same product name exists.
If it does, the product isn't stored in the database.
I know that the performance issue is the checking if the products exist, which takes longer and longer the more products are in the database. But there is no other way, I know, how I can improve this issue.
I may use indexes, but I don't know how to in this scenario.
If you have other ideas how to improve performance please feel free to comment your ideas.
tldr: I want to create testdata but it does take too long because it is checking if the products already exist. Want to improve performance.
Here is my code:
public String insertProdukt(String name, Double preis, Integer kat_id) throws SQLException, ClassNotFoundException {
Connection connection = ConnectionUtils.createNewConnection();
// does the product exist?
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select * from pro_produkte where pro_name=\"" + name + "\" AND pro_preis=\"" + preis + "\" AND pro_kat_id=\"" + kat_id + "\"");
if (resultSet.next()) {
//it does exist
System.out.println("Produkt: " + resultSet.getString("pro_name") + " existiert bereits");
} else {
//it dosen't -> insert into database
String sql = "Insert INTO pro_produkte (pro_name, pro_preis, pro_kat_id)"
+ "VALUES (\"" + name + "\", \"" + preis + "\", \"" + kat_id + "\")";
statement.executeUpdate(sql);
System.out.println("Produkt: " + name + " erstellt");
}
resultSet.close();
statement.close();
connection.close();
return null;
}
Thanks!
Instead of simply INSERT ..., use
INSERT IGNORE ...
And have a UNIQUE (or PRIMARY) that will catch the "duplicate".
INSERTing one row at a time is about 10 times as slow as inserting 100 rows at a time. So, if you are generating them by code, do
INSERT IGNORE INTO t
(col1, col2, ...)
VALUES
(1,2,...),
(22,55,...),
... ;
Or
LOAD DATA LOCAL INFILE '...' IGNORE ...
if reading from a file.
First thing - do not open a connection for every insert, unless you are using a connection pool.
Second thing - use PreparedStatement. Not only will this save you from SQL injection, it will also make it faster because it will avoid repetitive parsing.
Third thing - use PreparedStatement.addBatch() and commit a batch every 5000 rows (or something like that). This implies you use the same Connection and PreparedStatement for all inserts.
Fourth thing - if you are only filling the database with test data and you know that your test data is unique, create index AFTER you insert all the records. It will be significantly faster.
Fifth thing - if you are using InnoDB, make sure you have enough buffer space to keep entire index in memory, and put the database on SSD (~30x faster than HDD).
If you can do it outside Java, you can use database's proprietary features for bulk loading, restoring from backups or snapshots. Check what features your database provides.

SQL syntax error on WHERE clause using Netbeans IDE / Derby

the following line is the cause of the error, but I fail to spot where specifically it is wrong.
PreparedStatement stmt = connection.prepareStatement("SELECT (SEATS - RESERVATIONS) AS AVAIL FROM RESERVATIONS "
+ " CROSS JOIN (SELECT COUNT(FACULTY) WHERE FACULTY = ? AND DATE = ?) "
+ " WHERE SEATS = ?");
Followed by the error,
java.sql.SQLSyntaxErrorException: Syntax error: Encountered "WHERE" at line 1, column 93.
at org.apache.derby.client.am.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.ClientConnection.prepareStatement(Unknown Source)
at ReservationEntry.reserveRoom(ReservationEntry.java:30)
Reservation Entry is the file that the prepared statement is in, any help would be appreciated.
You need a FROM clause! Some databases require them. Something like this,perhaps:
SELECT (f.SEATS - r.RESERVATIONS) AS AVAIL
FROM RESERVATIONS R CROSS JOIN
(SELECT COUNT(FACULTY) as SEATS
FROM <table name goes here>
WHERE FACULTY = ? AND DATE = ?
) F
WHERE SEATS = ?;
I doubt this does anything useful, though, other than fix the syntax errors. You should ask a question with sample data, desired results, and an appropriate database tag.

java.sql.SQLException: ORA-00928: missing SELECT keyword

I am getting the above mentioned exception while executing the SQL query in Java.
statement2.executeUpdate("INSERT INTO visit_header"
+ "VALUES('"+visitnumber+"','"+date+"','"+cookie+"','"+ip+"')");
I want to know where it is going wrong.
As per initial look, you have a problem in your sql query:
statement2.executeUpdate("INSERT INTO visit_header" + "VALUES
Should be
statement2.executeUpdate("INSERT INTO visit_header " + "VALUES //Note space after header
There was no space between visit_header and VALUES, so your query was like this:
INSERT INTO visit_headerVALUES
Which is wrong.
You forgot to put space between visit_header and values:
statement2.executeUpdate("INSERT INTO visit_header" + " VALUES ('"+visitnumber+"','"+date+"','"+cookie+"','"+ip+"')");

Categories

Resources