0

I am trying to get the role variable from the Database, I am getting error that "Unhandled exception type SQL Exception"

static int indexValue=0;
public static int getRole(IndexBean w){
    String elid= IndexBean.getId();
           conn = ConnectionProvider.getCon();

           Statement statement = conn.createStatement();
           ResultSet resultset = statement.executeQuery("SELECT role FROM users WHERE elid = '" + elid + "'") ; 
           String role =  resultset.getString(1);
           if(role=="ADMIN"){
               indexValue = 1;
           }else if(role=="analyst"){
               indexValue = 2;
           }
           conn.close();
    return indexValue;
}

}

Saidu
  • 103
  • 1
  • 5
  • Unrelated, but: `role=="ADMIN"` is not doing what you think it does. –  Jul 18 '17 at 18:31

1 Answers1

0

The method executeQuery(String) of Statement interface has a checked exception of SQLException.

In your snippet, the line

ResultSet resultset = statement.executeQuery("SELECT role FROM users WHERE elid = '" + elid + "'");

has a checked exception which is not handled. This is why you are getting the unhandled exception message. Whenever you are calling this method, you need to either put it in a try-catch block or add throws SQLException in the containing method signature.

See Java Sql Statement API here.

Aritra
  • 131
  • 3
  • 7