Query syntax error with RFID code filter

Asked

Viewed 44 times

0

Syntax SQL error on the part from estoque where cod_rfid = 102.
Note: 102 is the value that is in the RFID tag.

    String dadoRFID = new String(readBuffer);
    String Del_inc = dadoRFID.substring(0,1); //Pega o primeiro caractere
    String Del_local = dadoRFID.substring(5,6); //Pega o primeiro caractere

    System.out.println(Del_inc); 
    System.out.println(Del_local); 

    if((Del_inc.equals("#")) && (Del_local.equals("&"))  ) { //Se caracteres OK.
        msg_rec_str = dadoRFID.substring(6, 12); //Pega os prox 6 caracteres após o "#"
        loc_est_str = dadoRFID.substring(1,5);
    }else {
        System.out.println("Não encontrou");
    }

    System.out.println("MSG"+ msg_rec_str);
    System.out.println("teste"+ loc_est_str);
    try {
         Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sistemarfid?useUnicode=yes&characterEncoding=UTF-8&useSSL=false&useTimezone=true&serverTimezone=UTC", "root", "12345");
         Statement statement=con.createStatement();

         Resultset rs= (Resultset) statement.executeQuery("select from estoque where cod_rfid =" + msg_rec_str);
         ((ResultSet) rs).next();
         int codrfid = ((ResultSet) rs).getInt(3);

         Resultset rs2= (Resultset) statement.executeQuery("select from estoque where endereco_estoque =" + loc_est_str);
         ((ResultSet) rs2).next();
         int localrfid = ((ResultSet) rs2).getInt(4);

         Resultset rs3= (Resultset) statement.executeQuery("select from estoque where estatus =" + 0);
         ((ResultSet) rs3).next();
         int estatusrfid = ((ResultSet) rs3).getInt(5);

             String dado_bd = "0"+ codrfid; //Variavel com o 
             String loc = "0"+localrfid; //Var com o retorno do local do BD
             String status_bd = "0"+ estatusrfid;           

             if(loc.equals(loc_est_str)) {
               System.out.println("LOCAL CONFIRMADO");
        switch (status_bd) { //Faz buscar para trocar
        case "0":
            System.out.println("status = 1");
            String sql1 = "update estoque set estatus = 1";
            PreparedStatement stmt = con.prepareStatement(sql1);
            stmt.executeUpdate();
            //stmt.execute();
            stmt.close();

            con.close();

            break;
        case "1":
            System.out.println("status = 2");
            String sql2 = "update estoque set estatus = 1";
            PreparedStatement stmt2 = con.prepareStatement(sql2);
            stmt2.executeUpdate();
            //stmt.execute();
            stmt2.close();

            break;
        case "2":
            System.out.println("status = 1");

            String sql3 = "update estoque set estatus = 1";
            PreparedStatement stmt3 = con.prepareStatement(sql3);
            stmt3.executeUpdate();
            //stmt.execute();
            stmt3.close();

            break;
        default:System.out.println("Status nao confirmado");
            break;
        }

             }else {
               System.out.println("LOCAL ERRADO");
             }
  • Welcome to Stackoverflow Douglas. This field cod_rfid table estoque is numerical or text? What is the exact error message that appears?

  • @Carlosheuberger, don’t you want to post your solution as an answer? If you want, explain a little more about your suggestion, to make it more complete.

  • the cod_rfid field in the BD is as varchar and in the Java code is as String

2 answers

1

in their querys failed to inform what you want to select.

Example: SELECT nome_coluna FROM estoque WHERE ....

  • I tried to do it this way: Resultset rs= (Resultset) statement.executeQuery("select cod_rfid from stock Where cod_rfid =" + msg_rec_str); But it gave the following error java.sql.Sqlexception: Column Index out of range, 3 > 1.

  • This error is given because you are trying to access an index that does not exist.

  • And how do I compare a Java variable with data in the BD ?

1

In the queries it was left to say which column you want to select. And this should be done before "FROM". To not give the error of "Column Index out" try to do as follows:

 "SELECT nome_tabela.nome_coluna FROM estoque WHERE cod_rfid ="
  • Use a point between the table name and the column name to specify more precisely what you want to see.

  • There are two other queries with the same mistake.

  • did so: Resultset rs= (Resultset) statement.executeQuery("select from stock.cod_rfid Where cod_rfid =" + msg_rec_str); and it still didn’t work

Browser other questions tagged

You are not signed in. Login or sign up in order to post.