2017-07-18 58 views
0

我想從數據庫中的作用變量的值,我得到錯誤「未處理的異常類型的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」'是不是做了什麼,你認爲它。 –

回答

0

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

相關問題