如何检查我的表中是否存在变量?

时间:2012-04-29 18:11:53

标签: java mysql methods sqlexception

我想浏览表格以找到一些值并将其与输入的值进行比较。

此代码验证是否存在并比较这两个值:

public  boolean verifer(){
    DataBase s = DataBase.getInstance();
     int numR1 =getnumR();

String req1 ="SELECT  `numR`  FROM  `reference` WHERE  `numR` =  '"+numR1+"' ";

    try {

        Statement m=  s.getConn().createStatement();
        ResultSet r1 = m.executeQuery(req1);

        int numR0 = 0;
        while (r1.next()) {

             numR0 =r1.getInt("numR");
            nbp++;   
        } 
        if(numR1==numR0){
            System.out.println("numR exists! : " +numR0 );
            verif=true;
             } 

        else { 
            System.out.println("numR is not existing ");
            verif=false;
              }

        rs1.close();
        m.close();

      } catch (SQLException e1) {
        e1.printStackTrace();
        System.out.println("error validation numR: "+e1);
        verif=false;

    } 

    return verif;
}

如果它们相等则通过将变量maxn与+1相加来插入请求。 如果不相等,则插入查询n = 1。

public  void remplir_reference(){

    DataBase s = DataBase.getInstance();

    String req3 ;

int numR  = getnumR();
int numA  = getnumA();
int numV  = getnumV();

String type_instal= setType_instal().toString();

String categorie=comboBox_categorie.getSelectedItem().toString();

Calendar calendar =new GregorianCalendar();
calendar.setTime(new Date());
int annee =calendar.get(Calendar.YEAR);

System.out.println(annee);


if(verif==true ){
       int maxn=maXnumR();
       int maxnplus = maxn +1 ;
       System.out.println("existe");
req3 ="INSERT INTO `reference` values ('"+numR+"','"+maxnplus+"','"+cin+"','"+numA+"','"+numV+"','"+type_instal+"','"+categorie+"','"+annee+"')";


            try {

                Statement m=  s.getConn().createStatement();
                m.executeUpdate(req3);

              } catch (SQLException e1) {
                e1.printStackTrace();
                System.out.println("remplir_reference si v=vrai: "+e1);

               }

      }


 if(verif ==false) {
     System.out.println("n'existe pas");

 req3 ="INSERT INTO `reference` values ('"+numR+"',1,'"+cin+"','"+numA+"','"+numV+"','"+type_instal+"','"+categorie+"','"+annee+"')";   
        try {

            Statement m=  s.getConn().createStatement();
            m.executeUpdate(req3);

          } catch (SQLException e1) {
            e1.printStackTrace();
            System.out.println("remplir_reference si v=false: "+e1);

           }
   }

}

它始终显示“不存在”,表格中的值n = 1

public  int maXnumR(){
    DataBase s = DataBase.getInstance();

    int numR= getnumR();


String req1 = "SELECT MAX('idrf') FROM `reference` WHERE `numR` = '"+numR+"'  ";


try {

    Statement m=  s.getConn().createStatement();
    ResultSet r1 = m.executeQuery(req1);

    while (r1.next()) {

         maxnumR =r1.getInt("idrf");
        nbp++;  
    } 

  } catch (SQLException e1) {
    e1.printStackTrace();
    System.out.println("maXnumR repetetion : "+e1);

} 

return maxnumR;
}

1 个答案:

答案 0 :(得分:1)

当numR为int时,您尝试添加''时查询不正确。

改变
String req1 ="SELECT  `numR`  FROM  `reference` WHERE  `numR` =  '"+numR1+"' ";

String req1 ="SELECT  numR  FROM  `reference` WHERE  numR =  "+numR1+" ";