如果字符串等于"godkjent",则尝试使此方法返回true.如果不是,则为false.这与我的数据库链接 [英] Trying to make this method to return true if the string equals to "godkjent" and false if not. This is linked up with my database

查看:61
本文介绍了如果字符串等于"godkjent",则尝试使此方法返回true.如果不是,则为false.这与我的数据库链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该方法应该从我的数据库(Godkjenning)中列出一个列表,如果我在数据库中写入IVE("Godkjent"),我希望它返回true,否则返回false.

This method is supposed to make a list from my database (Godkjenning), and I want it to return true if ive written ("Godkjent") in my database and false if not.

 public boolean GodkjenningT(String godkjentIkkegodkjent) {
    List<Tilbakemelding> tilbakemeldingListe = em.createQuery(
    "SELECT t FROM Tilbakemelding t WHERE t.Godkjenning LIKE :godkjenning" )
    .setParameter("godkjenning", godkjentIkkegodkjent )   
    .setMaxResults(1)
    .getResultList();
    System.out.println("godkjenning" + godkjentIkkegodkjent);
    if(tilbakemeldingListe.equals("Godkjent")){
        return true;
    }
    else {
        return false;
}
}
}

推荐答案

我将编辑如下代码:

public boolean GodkjenningT(String godkjentIkkegodkjent) {
    /*now the query matches cases and the 'LIKE' statement will work correctly
    having the '%' char before and after the string */
    List<Tilbakemelding> tilbakemeldingListe = em.createQuery(
    "SELECT t FROM Tilbakemelding t WHERE UPPER(t.Godkjenning) LIKE UPPER(?)")
    .setString(0, "%"+godkjentIkkegodkjent+"%")
    .getResultList();

    System.out.println("godkjenning" + godkjentIkkegodkjent);
    /*here you have to iterate the list to find if there is the string inside*/
    for(Tilbakemelding element : tilbakemeldingListe){
    /*if you want not to consider the case, you have to use the equalsIgnoreCase statement*/
        if(element.getGodkjenning().equals("Godkjent")){
            return true;
        } else {
            return false;
        }
     }
}

但是,请您发布以下课程的实现:Tilbakemelding?我以为您的Tilbakemelding实体(是一个实体吗?:))具有getGodkjenning()方法来完成我的代码.

But, can you please post the implementation of the class: Tilbakemelding? I've supposed your Tilbakemelding entity (is that an entity? :)) has a getGodkjenning() method to complete my code.

这篇关于如果字符串等于"godkjent",则尝试使此方法返回true.如果不是,则为false.这与我的数据库链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆