无法从数据库中检索得到的错误为“未处理的异常类型的SQL异常”

问题描述:

我想从数据库中的作用变量的值,我得到错误“未处理的异常类型的SQL异常”无法从数据库中检索得到的错误为“未处理的异常类型的SQL异常”

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

}

+0

无关,但:'作用== “ADMIN”'是不是做了什么,你认为它。 –

Statement接口的executeQuery(String)方法检查了SQLException异常。

在你的代码片段,行

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

有一个检查异常未被处理。这就是为什么你会得到未处理的异常消息。 无论何时调用此方法,都需要将其放入try-catch块或在包含的方法签名中添加抛出SQLException。

See Java Sql Statement API here.

+0

是的,抛出SQLException清除了我的问题。 谢谢:) – saidu941