Error doing email query in bank

Asked

Viewed 53 times

1

I am getting an error in Netbeans when making a query in an SQL database that validates user login from email:

package Modelo;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SqlUsuarios extends Coneccao {

    public boolean registrar(Usuarios usr) throws SQLException {
        PreparedStatement ps = null;
        Connection con = getConeccao();

        String sql = "INSERT INTO usuarios (nome, email, password, id_tipo) VALUES (?,?,?,?)";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(1, usr.getNome());
            ps.setString(2, usr.getEmail());
            ps.setString(3, usr.getPassword());
            ps.setInt(4, usr.getId_tipo());
            ps.execute();
            return true;

        } catch (SQLException ex) {
            Logger.getLogger(SqlUsuarios.class.getName()).log(Level.SEVERE, null, ex);
        };
        return false;
    }

    public boolean login(Usuarios usr) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        Connection con = getConeccao();

        String sql = "SELECT nome, email, password, id_tipo, FROM usuarios WHERE email like ? ";
        try {
            ps = con.prepareStatement(sql);
            ps.setString(2, usr.getEmail()); // ifnot put '3'
            rs = ps.executeQuery();

            if (rs.next()) 
            {
                if (usr.getPassword().equals(rs.getString(3))) //4º elemento na linha ...= "SELECT id, nome, email, password, id_tipo, FROM ...  
                {
                    //posições na linha está no numero. 


                    usr.setNome(rs.getString(1));
                    usr.setId_tipo(rs.getInt(4));
                    return true;
                } 
                else 
                {
                    return false;
                }

            }
            return false;

        } catch (SQLException ex) {
            Logger.getLogger(SqlUsuarios.class.getName()).log(Level.SEVERE, null, ex);
            return false;
        }

    }

The mistake:

java.sql.Sqlexception: Parameter index out of range (2 > number of Parameters, which is 1)

1 answer

3


I believe error lies in this line:

ps.setString(2, usr.getEmail());

You only pass an argument in the query: "SELECT nome, email, password, id_tipo, FROM usuarios WHERE email like ? ". Therefore, it should be 1 there:

ps.setString(1, usr.getEmail());

I also suggest removing the comma after id_tipo, not to give syntax error in mysql.

  • 1

    Take my +1..=]

  • @acklay I didn’t think so, but when it comes to Got the guys is pretty "sensitive" with this kkk information

Browser other questions tagged

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