Ciao a Tutti,
Questo è il mio codice rest:
@POST
@Path("/createSkill")
@Produces(MediaType.APPLICATION_JSON)
public Response createSkill() {
Skill skill = new Skill();
SkillJdbcImpl skillJdbcImpl = new SkillJdbcImpl();
skillJdbcImpl.creaSkill(skill);
return Response.ok().entity(skillJdbcImpl).build();
}
Questo è il mio codice creaSkill in jdbc:
@Override
public void creaSkill(Skill skill) {
String queryAddList = "insert into SKILL (id_SKILL, NOME_SKILL, CATEGORIA_SKILL) values(?,?,?)";
Connection dbConnection = null;
Statement statement = null;
PreparedStatement pStatement = null;
try {
dbConnection = Database.getConnection();
statement = dbConnection.createStatement();
// esegui istruzione query
pStatement = dbConnection.prepareStatement(queryAddList);
pStatement.setInt(1, this.rangeMaxId()); // aggiornamento Id automatico
pStatement.setString(2, skill.getNomeSkill());
pStatement.setString(3, skill.getCategoriaSkill());
pStatement.executeUpdate(); // aggiorna la lista --> return Int
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (dbConnection != null && statement != null && pStatement != null) {
dbConnection.close();
statement.close();
pStatement.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
Questo è il codice Angular js:
(function () {
var app = angular.module("myApp");
app.controller("createController", function($scope, $http){
$scope.createSkill = function () {
$http.post('http://localhost:8900/WebServiceRestFull/createSkill', {"id_skill": $scope.idSkill,"nomeSkill": $scope.idNomeSkill,"categoriaSkill": $scope.idCategoriaSkill})
.success(function(data, status, headers, config) {
alert("Skill aggiunto correttamente!");
}).error(function(data, status, headers, config) {
console.log("error");
});
};
});
})();
Questo è html:
<div class="col-md-8 text-center " style=" margin: 0 auto;">
<form class="list-group-item list-group-item-primary">
<div class="form-row">
<div class="form-group col-md-3">
<label for="formGroupId">Id Skill:</label>
<input type="text" class="form-control" name="skill" ng-model="idSkill" required minlength="1" maxlength="3" pattern="[0-9]+" >
</div>
<div class="form-group col-md-3" >
<label for="formGroupNome">Nome skill:</label>
<input type="text" class="form-control" name="nomeSkill" ng-model="idNomeSkill" required minlength="1" maxlength="10" pattern="[a-zA-Z ]*">
</div>
<div class="form-group col-md-3">
<label for="formGroupCategoria">Categoria skill:</label>
<input type="text" class="form-control" name="categoriaSkill" ng-model="idCategoriaSkill" required minlength="1" maxlength="10" pattern="[a-zA-Z ]*">
</div>
<div class="form-group col-md-3">
<label for="formGroupCategoria">Crea skill:</label><br>
<button type="button" ng-click="createSkill()">crea</button>
</div>
</div>
<div ng-if="idSkill && idNomeSkill && idCategoriaSkill" >
<h4 >Tutti i campi sono corretti!!</h4>
</div>
</form>
</div>
Ma non mi funziona come mai???