I'm making a rest API for an App I'm creating, I have a MySQL database and I'm using Springboot. I coded this
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import java.sql.ResultSet;
import java.sql.SQLException;
public class MySQLInfosGateway implements InfosGateway {
private NamedParameterJdbcTemplate jdbcTemplate;
public MySQLInfosGateway(NamedParameterJdbcTemplate jdbcTemplate){
this.jdbcTemplate=jdbcTemplate;
}
@Override
public Personne getInfos(String badge){
var query = "select NOM, PRENOM from PERSONNEL where BADGE = "+badge;
var result = jdbcTemplate.query(query, new ResultSetExtractor<Object>() {
@Override
public Object extractData(ResultSet resultSet) throws SQLException, DataAccessException {
resultSet.next();
return new Personne(resultSet.getString("NOM"),resultSet.getString("PRENOM"));
}
});
return (Personne) result;
}
}
Here's my Personneclass :
package com.piroux.phoenixrhbackend.domain.entities;
public class Personne {
private String nom;
private String prenom;
public Personne(String nom, String prenom){
this.nom=nom;
this.prenom=prenom;
}
public String getPrenom() {
return prenom;
}
public String getNom() {
return nom;
}
public void setPrenom(String prenom) {
this.prenom = prenom;
}
public void setNom(String nom) {
this.nom = nom;
}
}
and I'm trying with the following request on Postman:
localhost:8080/fonction-recup-infos/nom-prenom?badge=50387
If I execute the request select NOM, PRENOM from PERSONNEL where BADGE = 50387
- In a SQL script => I get the correct
NOMandPRENOM; - With my API => I get
nullandnull
For you to know, BADGE is a unique String, so there's only one NOM and PRENOM for each BADGE.
It's my first time creating a REST API so if any information is missing please tell me
@Bean public InfosGateway infosGateway(){return new MySQLInfosGateway(jdbcTemplate);}queryvariable after. BADGE is a unique String String literal value must be enclosed with quote chars. Testvar query = "select NOM, PRENOM from PERSONNEL where BADGE = '"+badge+"'";query = "select NOM, PRENOM from PERSONNEL where BADGE = 50387"which is exactly what I want it to bePersonneclass. The error might be in there as well.