sadurator
sadurator

Reputation: 111

How to fix ORA-01008 : Not all variables bound

I have a problem with this piece of code. When I pass user and pass arguments to isLogin fucntion it throws ORA-01008 errror. I am connected to Oracle database using jdbc.


 public boolean isLogin(Connection conn, String user, String pass) throws SQLException{
   String sql = "SELECT * FROM PRACOWNIK WHERE imie =? AND nazwisko =? ";
        PreparedStatement stmt;
        ResultSet rs;

        try {
            stmt = conn.prepareStatement(sql);
            stmt.setString(1, user);
            stmt.setString(2, pass);
            rs = stmt.executeQuery(sql);

            if(rs.next()){
                return true;
            }
            else {
                return false;
            }


        } catch (SQLException e){
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("Error ");
            alert.setContentText(e.getMessage());
            alert.showAndWait();
            return false;
        }

    }

I use this function in Controller class

public class Controller implements Initializable{

    public Pracownik pracownik = new Pracownik();

    @FXML
    private Label isConnected;

    @FXML
    private TextField txtUsername;

    @FXML
    private TextField txtPass;

    private Connection conn;

   // private ObservableList<Pracownik> lista = FXCollections.observableArrayList();

    public void initialize(URL url, ResourceBundle rb){
        conn = DBConnection.getConnection();

     //   lista = new Pracownik().getAll(conn);
    }

    public void login(ActionEvent event){
        try {
            if(pracownik.isLogin(conn, txtUsername.getText(), txtPass.getText())){
            isConnected.setText("Correct");
            }
            else{
             isConnected.setText("False");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }
}

And this is a error message

Caused by: Error : 1008, Position : 0, Sql = SELECT pesel FROM PRACOWNIK WHERE imie =:1  AND nazwisko =:2  , OriginalSql = SELECT pesel FROM PRACOWNIK WHERE imie =? AND nazwisko =? , Error Msg = ORA-01008: not all variables bound

When I use normal Select query just to print the table everything is fine.

Upvotes: 0

Views: 4042

Answers (1)

The Impaler
The Impaler

Reputation: 48770

You should NOT specify the SQL query again. It's already specified. Change the line:

rs = stmt.executeQuery(sql); // method from java.sql.Statement

to:

rs = stmt.executeQuery(); // method from java.sql.PreparedStatement

The first method does not take parameters into consideration and runs the SQL "as is"... and therefore you get the error you mention.

Upvotes: 4

Related Questions