2015-04-06 63 views
0

我正在學校的高級項目上工作,我遇到了問題。出於某種原因,當我嘗試將用戶的名字和姓氏從我們的數據庫中刪除時,我一直收到空。我知道JDBC正常工作,因爲我們的登錄工作正常。這是我的一些代碼,可以告訴你發生了什麼事情。Android Studio Oracle JDBC,結果集保持爲空

賬戶片段

public class AccountFragment extends Fragment { 
View rootview; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    rootview = inflater.inflate(R.layout.fragment_account, container, false); 
    return rootview; 
} 

public void onViewCreated(View view, Bundle savedInstanceState){ 
    super.onViewCreated(view, savedInstanceState); 
    // initialize your views 

    TextView mTextView1 = (TextView)view.findViewById(R.id.user_name); 
    mTextView1.setText(UserLoginInfo.fName + " " + UserLoginInfo.lName); 


    TextView mTextView2 = (TextView)view.findViewById(R.id.user_email); 
    mTextView2.setText(UserLoginInfo.userEmail); 


} 

}

UserLoginInfo類

public class UserLoginInfo { 
public static String userEmail; 
public static String fName; 
public static String lName; 


public UserLoginInfo() throws SQLException { 

    try { 
     Connection conn = ConnectionManager.getConnection(); 

     Statement st = null; 

     st = conn.createStatement(); 

     ResultSet resultSet = null; 

     if(st!=null) 
      resultSet = st.executeQuery("SELECT * FROM userinfo WHERE email=" + userEmail + "'"); 
     if(resultSet!=null) { 
      while (resultSet.next()) { 
       fName = resultSet.getString("firstname"); 
       lName = resultSet.getString("lastname"); 
      } 
     } 

     resultSet.close(); 
     st.close(); 
    } catch(SQLException e) { 
     e.printStackTrace(); 
    } 



} 

記住登錄不工作。所以我不明白爲什麼我一直爲fName和lName獲得空值。任何幫助將不勝感激。謝謝!!

+0

ResultSet.next()移到下一行。我想,所有相應的數據都在同一行。所以你不需要第二次。 – Qualtagh

+0

是的,我已經嘗試在單獨的課程中做到這一點,它仍然無法正常工作。 –

+0

請將您的查詢添加到問題中。 – Qualtagh

回答

0

在查詢字符串"SELECT * FROM userinfo WHERE email=" + userEmail + "'"中,有一個'missing'。

順便說一句,使用executeQuery就像這樣是一個可怕的想法。它打開了SQL注入攻擊的大門。改用準備好的陳述。 Prepared Statement Usage.

+0

感謝您注意,但它仍然無效。我對使用JDBC還是一種新嘗試,但我可能會嘗試使用prepared語句。 –

+0

你可以在獨立的Java應用程序中運行UserLoginInfo類,因爲它不依賴於其他Android相關的東西,並且看看結果會是什麼? – user245259