Error updating a record of a DB in SQLServer from JAVA [closed]

0

I'm trying to make a system in JAVA for hotel room reservations, I'm really new to this, but here I go that I want.

Well in JAVA I have everything sorted in packages, a package called Data with all the classes that contain the data of my tables in SqlServer to then make forms from JAVA to enter, update and delete any data from the system.

Another package called Logica where all the functions are from the function that allows me to connect to the Database and another call Presentation to which will have the forms to manipulate from there the Database and finally one called Files that contains the images of the buttons some label and the menu bar of my system.

I am starting to realize the system, I have started to make the form to enter the rooms, making a call to the class fhabitación which contains all the functions to insert, edit and delete data from my room table that is in the database in SQLServer.

The point is that I already tried the connection and everything "OK", I could also insert and delete a room from the form made in JAVA and also let me do it perfectly, the problem is when I try to update a data of the room table from the form, send me a message that says the following:

I do not know what it is, I am updated the table according to the room which is entered into the table automatically since it has the identity property which makes an AUTO_INCREMENT, then shared the code of my function fhabitacion:

public class fhabitacion {
    // Declaración de variables
    private conexion sqlserver = new conexion();
    private Connection cn = sqlserver.conectar();
    private String sSQL = "";
    public Integer totalregistros;

    public DefaultTableModel mostrar (String buscar){
        DefaultTableModel modelo;

        String [] titulo = {"ID","Número","Piso","Descripción","Caracteristicas","Precio","Estado","Tipo habitación"};

        String [] registro = new String[8];

        totalregistros=0;

        modelo = new DefaultTableModel(null,titulo);

        sSQL="select * from habitacion where piso like '%"+ buscar + "%' order by idhabitacion";

        try {
            Statement st = cn.createStatement();
            ResultSet rs = st.executeQuery(sSQL);

            while(rs.next()){
                registro [0]=rs.getString("idhabitacion");
                registro [1]=rs.getString("numero");
                registro [2]=rs.getString("piso");
                registro [3]=rs.getString("descripcion");
                registro [4]=rs.getString("caracteristicas");
                registro [5]=rs.getString("precio_diario");
                registro [6]=rs.getString("estado");
                registro [7]=rs.getString("tipo_habitacion");

                totalregistros=totalregistros+1;
                modelo.addRow(registro);
            }
            return modelo;
        } catch (Exception e) {
            JOptionPane.showConfirmDialog(null, e);
            return null;
        }
    }

    public boolean insertar(vhabitacion dts){
        sSQL="insert into habitacion (numero,piso,descripcion,caracteristicas,precio_diario,estado,tipo_habitacion)" + 
                "values (?,?,?,?,?,?,?)";
        try {

            PreparedStatement pst = cn.prepareStatement(sSQL);
            pst.setString(1, dts.getNumero());
            pst.setString(2, dts.getPiso());
            pst.setString(3, dts.getDescripcion());
            pst.setString(4, dts.getCaracteristica());
            pst.setDouble(5, dts.getPrecio_diario());
            pst.setString(6, dts.getEstado());
            pst.setString(7, dts.getTipo_habitacion());

            int n = pst.executeUpdate();

            if(n!=0){
                return true;
            }
            else{
                return false;
            }

        } catch (Exception e) {
            JOptionPane.showConfirmDialog(null, e);
            return false;
        }
    }

    public boolean editar(vhabitacion dts){
        sSQL="update habitacion set numero=?,piso=?,descripcion=?,caracteristicas=?,precio_diario=?,estado=?,tipo_habitacion=?"+
                "where idhabitacion=?";

        try {

            PreparedStatement pst = cn.prepareStatement(sSQL);
            pst.setString(1, dts.getNumero());
            pst.setString(2, dts.getPiso());
            pst.setString(3, dts.getDescripcion());
            pst.setString(4, dts.getCaracteristica());
            pst.setDouble(5, dts.getPrecio_diario());
            pst.setString(6, dts.getEstado());
            pst.setString(7, dts.getTipo_habitacion());
            pst.setInt(8, dts.getIdhabitacion());

            int n = pst.executeUpdate();

            if(n!=0){
                return true;
            }
            else{
                return false;
            }
        } catch (Exception e) {
            JOptionPane.showConfirmDialog(null, e);
            return false;
        }
    }

    public boolean eliminar(vhabitacion dts){
        sSQL="delete from habitacion where idhabitacion=?";
        try {

            PreparedStatement pst = cn.prepareStatement(sSQL);
            pst.setInt(1, dts.getIdhabitacion());

            int n = pst.executeUpdate();

            if(n!=0){
                return true;
            }
            else{
                return false;
            }
        } catch (Exception e) {
            JOptionPane.showConfirmDialog(null, e);
            return false;
        }
    }
}
    
asked by yasser33 17.10.2017 в 20:01
source

1 answer

1

Try adding a space before the where in this line of code:

 sSQL="update habitacion set numero=?,piso=?,descripcion=?,caracteristicas=?,precio_diario=?,estado=?,tipo_habitacion=?"+
            " where idhabitacion=?";
    
answered by 18.10.2017 / 05:56
source