0

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 NOM and PRENOM;
  • With my API => I get null and null

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

8
  • how did you instantiate your gateway? Commented May 18, 2022 at 6:39
  • @Stultuske @Bean public InfosGateway infosGateway(){return new MySQLInfosGateway(jdbcTemplate);} Commented May 18, 2022 at 6:41
  • var query = "select NOM, PRENOM from PERSONNEL where BADGE = "+badge; - show the value of query variable after. BADGE is a unique String String literal value must be enclosed with quote chars. Test var query = "select NOM, PRENOM from PERSONNEL where BADGE = '"+badge+"'"; Commented May 18, 2022 at 6:42
  • @Akina I ran in debug and I got query = "select NOM, PRENOM from PERSONNEL where BADGE = 50387" which is exactly what I want it to be Commented May 18, 2022 at 6:46
  • Could you also add your Personne class. The error might be in there as well. Commented May 18, 2022 at 6:50

2 Answers 2

1

A couple of things wrong with your code. First never use concatenation to create a query string based on user input. It is dangerous. Second I would suggest using the RowMapper instead of the ResultSetExtractor it is easier to use.

You are using getString("<column-name>") if your database doesn't expose the metadata this won't work and you have to use positional identifiers.

All in all I suggest you do this.

@Override
public Personne getInfos(String badge){
    var query = "select NOM, PRENOM from PERSONNEL where BADGE = :badge";
    var result = jdbcTemplate.queryForObject(query, Map.of("badge", badge), (rs, rowNum) -> 
    new Personne(rs.getString("NOM"), rs.getString("PRENOM"));        
    return result;
}

NOTE: You also might want to try nom and prenom as the column names, yuo are using MySQL which can be a bit picky about casing (depending on your configuration of MySQL and the platform you are running on).

Sign up to request clarification or add additional context in comments.

5 Comments

I tried your solution; but I get an error on the *jdbcTemplate.query. It tells me no suitable method found for query(java.lang.String,java.lang.String,(resultSet[...]OM"))) Plus the getString method of rs is in red but I don't know what's wrong with it
You looked at my old answer, I changed query to queryForObject which is the correct one) as well as the change in the signature (it needs another parameter for the lambda) and it needs parameters in the other way.
I Still get the same kind of problems : queryForObject is underline in red and it shows Cannot resolve method 'queryForObject(java.lang.String, <lambda expression>)' and rs.getString is still in red and i can "rename reference" but it doesn't change anything EDIT : plus result type is now void
A crap my bad. You are using the NamedParameterJdbcTemplate and not the JdbcTemplate. Updated (again).
Now It works; I check in debug mode; figured out My original code worked and the error was elsewhere. In fact I didn't return the right Object in another method of anorther class. But anyway, thanks for your help as now my code is cleaner than before :D
0

Change this line from

 var query = "select NOM, PRENOM from PERSONNEL where BADGE = "+badge;

to

 var query = "select NOM, PRENOM from PERSONNEL where BADGE = '" + badge + "'";

It might be resolved.

5 Comments

I tried, but I still have null and null
let check something, modify sql by var query = "select 1 as abc, NOM, PRENOM from PERSONNEL where BADGE = '" + badge + "'"; and modify this line var result = jdbcTemplate.query(query, badge, (rs) -> new Personne(rs.getString("NOM"), rs.getString("PRENOM")); by var result = jdbcTemplate.query(query, badge, (rs) -> System.out.println(rs.getString("abc")); new Personne(rs.getString("NOM"), rs.getString("PRENOM")); Can you get the '1'?
I tried ; but I get an error on the jdbcTemplate.query. It tells me no suitable method found for query(java.lang.String,java.lang.String,<lambda expression>)) Plus the getString method of rs is in red but I don't know what's wrong with it
Oh sorry, My mistake. The code should be look like this ` jdbcTemplate.query(query, badge, (rs) -> { System.out.println(rs.getString("abc")); new Personne(rs.getString("NOM"), rs.getString("PRENOM")); } `
Still the same problem : no suitable method found for query(java.lang.String,java.lang.String,(rs)->{ Sy[...])); })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.