2015-04-06 62 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