无法从数据库中检索值,将错误视为"Unhandled exception type SQL Exception"



我正在尝试从数据库中获取角色变量,但收到错误"未处理的异常类型 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;
}

}

语句接口的 executeQuery(String( 方法具有 SQLException 的检查异常。

在您的代码段中,该行

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

有一个未处理的已检查异常。这就是您收到未处理的异常消息的原因。 无论何时调用此方法,都需要将其放在 try-catch 块中,或者在包含方法签名中添加抛出 SQLException。

请参阅此处的 Java SQL 语句 API。

相关内容

最新更新