【发布时间】:2015-04-06 23:49:06
【问题描述】:
我正在为学校完成我的高级项目,但遇到了问题。出于某种原因,当我尝试从我们的数据库中提取用户的名字和姓氏时,我一直为空。我知道 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 总是为空。任何帮助将不胜感激。谢谢!!
【问题讨论】:
-
ResultSet.next() 移动到下一行。我想,你所有的相应数据都在同一行。所以你不需要第二个 while。
-
是的,我已经尝试在单独的课程中这样做,但仍然没有用。
-
请将您的查询添加到问题中。
-
在设置 getString 后 fName 是否为空,或者只是因为那部分代码没有执行?这个函数返回什么:真还是假?
-
您可以从 SQL*Plus 或任何其他 DB 查看器执行查询吗?它是否返回
firstname的值?