When I attempt to read data from the access database there is no issue, e.g.
ResultSet equipmentData = DatabaseController.RunOperation("SELECT * FROM Equipamentos");
Code for DatabaseController:
package application;
import java.sql.*;
public class DatabaseController {
private static String databaseURL;
public static void setURL(String url) {
try {
databaseURL = "jdbc:ucanaccess://" + url + ";readonly=false";
} catch (Exception e) {
e.printStackTrace();
}
}
public static ResultSet RunOperation(String input){
ResultSet rs = null;
try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
Connection c = DriverManager.getConnection(databaseURL);
Statement st = c.createStatement();
rs = st.executeQuery(input);
}
catch(Exception e){
e.printStackTrace();
}
return rs;
}
}
However, when I try to do any operation which writes to the database it does not function. Specifically, I try to update a row with:
String operation = "UPDATE Equipamentos SET "
+ "CodigoEquipamento = '"+equipmentCode.getText()+"', "
+ "CodigoPrincipal = '"+equipType+"', "
+ "Equipamento = '"+equipmentDescription.getText()+"', "
+ "EquipamentoCritico = "+ criticalEquipment.isSelected() +", "
+ "Marca = '"+brand.getText()+"', "
+ "Tipo = '"+type.getText()+"', "
+ "NumeroSerie = '"+serialNumber.getText()+"', "
+ "OutrasCaracteristicas = '"+otherCharacteristics.getText()+"', "
+ "Observacoes = '"+observations.getText()+"' "
+ "WHERE CodigoEquipamento = '"+this.lastEquipmentCode+"'";
DatabaseController.RunOperation(operation);
which, when testing, results in the query
UPDATE Equipamentos SET CodigoEquipamento = 'R100.00', CodigoPrincipal = 'R100', Equipamento = 'Equipamento provisoriamente sem código', EquipamentoCritico = true, Marca = 'Código temporário', Tipo = 'null', NumeroSerie = 'null', OutrasCaracteristicas = 'Todas as Fichas de Trabalho feitas com este Código deverão ser enviadas de imediato para a DPA a fim de se atribuir um código', Observacoes = 'All Job Cards with this code must be sent to the DPA at once in order to attribute a new code' WHERE CodigoEquipamento = 'R100.00'
I've used this query(copy paste) in the access database, and it runs perfectly.
I have also tried deleting the row and inserting a new one with the edited values, but that also does not work.
The error that UCanAccess gives me is UCAExc:::5.0.1 General error.
Thank you for any help you can provide.
Using Erik A's suggestion I used a Prepared Statement. This did not fix my issue, however it pointed me to use .executeUpdate() instead of .executeQuery(). This fixed the error. This was because executeQuery() expected a result, as its typing is ResultSet.
Instead of creating the statement in another class I chose to create a function in the DatabaseControllerClass to handle it: