UCanAccess Cannot Write To Database, Although it can Read

382 views Asked by At

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.

1

There are 1 answers

0
Ben Burt On

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:

public static void saveToDatabase(String equipCode, String equipType, String equipDesc, Boolean critEquip, String brand, String type, String serialNum, String otherChar, String obs, String lastEquipCode) {
        try {
            Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
            Connection c = DriverManager.getConnection(databaseURL);
            PreparedStatement st = c.prepareStatement("UPDATE Equipamentos SET "
                    + "CodigoEquipamento = ?, "
                    + "CodigoPrincipal = ?, "
                    + "Equipamento = ?, "
                    + "EquipamentoCritico = ?, "
                    + "Marca = ?, "
                    + "Tipo = ?, "
                    + "NumeroSerie = ?, "
                    + "OutrasCaracteristicas = ?, "
                    + "Observacoes = ? "
                    + "WHERE CodigoEquipamento = ?");
            st.setString(1, equipCode);
            st.setString(2, equipType);
            st.setString(3, equipDesc);
            st.setBoolean(4, critEquip);
            st.setString(5, brand);
            st.setString(6,  type);
            st.setString(7, serialNum);
            st.setString(8, otherChar);
            st.setString(9, obs);
            st.setString(10, lastEquipCode);
            st.executeUpdate();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }