Before Insert trigger in Derby DB - mysql

I am building a javafx project using netbeans and am using derby database for that.
I have two tables, BOOK and ISSUE.
I want to create a BEFORE INSERT trigger ON ISSUE table which checks for the value of a boolean field called "available" in the BOOK table.
A button called BookIssue when pressed, should check the following trigger.
If the value of "available" is false, a warning message should pop up, else the insert operation must be executed.
I am not able to get the trigger command right.
String trigger = "CREATE TRIGGER toIssue NO CASCADE BEFORE INSERT ON ISSUE"
+ " FOR EACH ROW"
+ " BEGIN"
+ " SELECT isAvail from BOOK WHERE id = '"+ bookId + "'"
+ " IF isAvail = false"
+ " THEN RAISE_APPLICATION_ERROR(-20001,'Books Out of stock')"
+ " END IF"
+ " END";
databasehandler.execQuery(trigger);
I am getting following exception in my log:
Exception at execQuery:dataHandlerSyntax error: Encountered "BEGIN" at line 1, column 71.
Could someone help me with this! I couldn't find any other place which had a similar issue with derby.

I think you want: ExecuteNonQuery().
In addition, this line is suspect:
SELECT isAvail from BOOK WHERE id = '"+ bookId + "'"
The trigger body should look more like this:
DECLARE v_avail boolean;
SELECT b.isAvail INTO v_avail
FROM BOOK b
WHERE b.id = NEW.ID;
IF NOT v_isAvail THEN
RAISE_APPLICATION_ERROR(-20001,'Books Out of stock')"
END IF;

Related

Multi tables update using javafx and Mysql

I have to update a customer information that are spread over 4 Mysql tables. I created 1 Customer class. when I first add the information it is added to an observable list that populate a table, and by clicking on a selected row the information are displayed in textboxes to edit, but the updates are not being saved into the MySQL tables. Can you tell if it is from this part of code or is it coming from somewhere else in the program. What is wrong with my code ?
public void updateCustomer(Customer selectedCustomer, String user, LocalDateTime timePoint) throws Exception{
String query = "UPDATE customer, address, city, country"
+ " SET customer.customerName = '"+selectedCustomer.getCustomerName()+"', customer.lastUpdate = '" +timePoint+"', customer.lastUpdateBy = '"+user+ "', "
+ " address.address = '" +selectedCustomer.getAddress()+ "', address.address2 = '" +selectedCustomer.getAddress2()+ "', address.postalCode = '" +selectedCustomer.getPostalCode()+ "', address.phone = '" +selectedCustomer.getPhone()+ "', address.lastUpdate='" +timePoint+ "', address.lastUpdateBy = '" +user+ "', "
+ " city.city = '"+selectedCustomer.getCity()+"',city.lastUpdate='"+timePoint+"',city.lastUpdateBy = '"+user+ "', "
+ " country.country = '"+selectedCustomer.getCountry()+"',country.lastUpdate ='"+timePoint+"',country.lastUpdateBy = '"+user+ "' "
+ " WHERE customer.customerId = " +selectedCustomer.getCustomerId()+ " AND customer.addressId = address.addressId AND address.cityId = city.cityId AND city.countryId = country.countryId " ;
statement.executeUpdate(query);
}
What I usually do is:
Create my data class.
Create load and save methods on my DB class.
Set up the FXML so that the TableColumns show the right information from the data class
Get the data into an ObservableList from the database (I like to use Derby but that shouldn't make a difference) and put that into the table.
Add a listener to the selection model so that when the selected item in the table changes, the selected item is referenced by another variable (say "selectedCustomer" and that variable's data is shown into the editable textfields or comboboxes or whatever. Note that I don't use bindings when showing the selectedCustomer in the textboxes. I just use plain setTexts.
When the user clicks on Save or something, the data in the textfields are set into the selectedCustomer (for example, selectedCustomer.setName(nameText.getText());)
I call the database class' save method (for example, DB.save(selectedCustomer); )
That should do the trick! Never failed me yet.
However, I may be at fault here, since I couldn't be bothered to read your SQL statement. Please, for goodness sake, learn to use PreparedStatements! First, I don't really understand how your table is set up, so I can't really comment, but it's really hard to understand. However, if I were to take a wild guess, I think the problem may have something to do with this part here:
WHERE ... AND customer.addressId = address.addressId AND address.cityId = city.cityId AND city.countryId = country.countryId
I don't understand how that part works--either that SQL statement does not make sense or my SQL needs practice (probably the latter).
Since you use MySQL, how about you use a manager (such as PHPMyAdmin or since you are using Java, SQuirreL) to try executing your SQL statement manually and see what happens? If you enter the SQL statement and nothing changed (when there should be) then your SQL statement is at fault.

MYSQL new event if syntax in phpMyAdmin

I want to make an event that updates a fields in a table (start)
I created the event in phpMyAdmin and it gives me an error
BEGIN
IF ((select sum(putNextDay) from machineTypes where no=1) = 1) THEN
update prgs set start=CURDATE() where machineNo=1 and start=CURDATE() + INTERVAL 1 DAY and isDone=0;
END IF
END
it gives syntax error near END
on line 2 where you checked the if condition ,
it take your "if(xyz where=1)" condition as "if (xyz=1) " on your code,
it seem that you have assigned a value "1" in "select sum(putNextDay) from machineTypes where no" .
try to apply the same in ''.
hope this will helpful to you .
thanks

BEGIN AND COMMIT MYSQL EXECUTE JAVA

I have this simple doubt:
String sql ="BEGIN;" +
"DELETE FROM users WHERE username='gg';" +
"DELETE FROM comprofiler WHERE id=611;" +
"COMMIT;";
st.execute(sql);
Why doesn't it work? It works if it's just one instruction, how can I type this?

Undefined method 'join' during mysql action (ruby/sinatra)

Undefined method 'join' during mysql action (ruby/sinatra)
Code:
rs = con.query('select * from userlog')
#logentry = ""
rs.each_hash { |h|
#logentry = #logentry + "ID: " + h['Id'] + "User: " + h['user'] + " - Time: " + h['datetime'] + " - Description: " + h['description'] + "<br>"
}
Error:
undefined method `join' for #<String:0x007f70585b68f8>
when I add ".to_s" to the "h[Id]" then I get blank results for the ID but the rest is shown.
It sounds like your 'userlog' table column name for the identifier is not 'Id', maybe 'id'. Otherwise it would have been selected normally.
I had similar problem. The reason was that table name was incorrect in database, and for some reason MySQL error messages were incorrect. Check all database, table and variable names.

Simple trigger doesn't work

Hy. I am learning mysql and I have a simple trigger example in my book that doesn't work when I try it
create trigger newproduct after insert on products
for each row select "Product selected";
The error is :
Error Code: 1415. Not allowed to return a result set from a trigger
Why do i get this error? I am using mysql 5.2
Because you should set data to any var like
SET #trigger_var = (select "Product selected");