1

GET, PUT and DELETE work, but trying to do the POST method it doesn't work. In my data base, 'usuario' has an id, a name and age (edad).

@POST
@Consumes({MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public Response crearUsuario(Usuario usuario) {
    try {
        String sql = "INSERT INTO `RedLibros`.`usuario`(`nombre`,`edad`) " + "VALUES('"
    + usuario.getnombre() + "', '" + usuario.getedad() + "');";
        
        PreparedStatement ps = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        int affectedRows = ps.executeUpdate();
        
        ResultSet generatedID = ps.getGeneratedKeys();
        if (generatedID.next()) {
            usuario.setidusuario(generatedID.getInt(1));
            String location = uriInfo.getAbsolutePath() + "/" + usuario.getidusuario();
            return Response.status(Response.Status.CREATED).entity(usuario).header("Location", location).header("Content-Location", location).build();
        }
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("No se pudo crear el usuario").build();
        
    } catch (SQLException e) {
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity("No se pudo crear el usuario\n" + e.getStackTrace()).build();
    }
}

I'm using Postman and the body of my input is:

</usuario idusuario="1">
       <edad>43</edad>
       <nombre>George</nombre>
</usuario>

The error is: Ljava.lang.StackTraceElement;@1aeb297

And this is the script of my db, im not sure if its because the character set, im using UTF-8.

-- MySQL Script generated by MySQL Workbench
-- lun 19 abr 2021 14:13:50 CEST
-- Model: New Model    Version: 1.0
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

-- -----------------------------------------------------
-- Schema RedLibros
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `RedLibros` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `RedLibros` ;

-- -----------------------------------------------------
-- Table `RedLibros`.`usuario`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `RedLibros`.`usuario` (
  `idusuario` INT NOT NULL,
  `nombre` VARCHAR(45) NULL,
  `edad` VARCHAR(45) NULL,
  PRIMARY KEY (`idusuario`))
ENGINE = InnoDB;
  • Could you please add 1. input for POST 2. error message when you invoke POST, 3. Table structure 4. Table DB type 5 Table constraints? Looks like insert isnt working. – Koushik Roy Apr 19 '21 at 11:55
  • I thought it was the SQL sentence, but i can't see where its wrong. – Hugo Álvarez Apr 19 '21 at 12:20
  • I think you are not passing `idusuario` from POST call. If you expect this to be auto increment, then you need to define the table accordingly. You should use auto_increment syntax like below - CREATE TABLE IF NOT EXISTS `RedLibros`.`usuario` ( `idusuario` INT NOT NULL AUTO_INCREMENT, `nombre` VARCHAR(45) NULL, `edad` VARCHAR(45) NULL, PRIMARY KEY (`idusuario`)) – Koushik Roy Apr 19 '21 at 13:00
  • It was the problem, thank you very much! It works now :) – Hugo Álvarez Apr 19 '21 at 13:30
  • Marked it as answer... Ty – Koushik Roy Apr 19 '21 at 20:26

1 Answers1

0

I think you are not passing idusuario from POST call. If you expect this to be auto increment, then you need to define the table accordingly. You should use auto_increment syntax like below -

CREATE TABLE IF NOT EXISTS RedLibros.usuario ( idusuario INT NOT NULL AUTO_INCREMENT, nombre VARCHAR(45) NULL, edad VARCHAR(45) NULL, PRIMARY KEY (idusuario));
Koushik Roy
  • 6,868
  • 2
  • 12
  • 33