0

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

此代码用于验证它是否存在并比较两个值​​​:

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;
}
4

1 回答 1

1

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

从改变

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

String req1 ="SELECT  numR  FROM  `reference` WHERE  numR =  "+numR1+" ";
于 2012-04-29T18:18:22.763 回答