【问题标题】:Android Studio Oracle JDBC, keep getting null for resultSetAndroid Studio Oracle JDBC,结果集一直为空
【发布时间】: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 的值?

标签: java android oracle jdbc


【解决方案1】:

在查询字符串"SELECT * FROM userinfo WHERE email=" + userEmail + "'"中,缺少一个'。

顺便说一句,像这样使用executeQuery 是一个糟糕的主意。它为 SQL 注入攻击打开了大门。改用准备好的语句。 Prepared Statement Usage.

【讨论】:

  • 感谢您注意到这一点,但它仍然无法正常工作。我对使用 JDBC 还是有点陌生​​,但我可能会尝试使用准备好的语句。
  • 你能在一个独立的java应用程序中运行UserLoginInfo类吗,因为它不依赖于其他与android相关的东西,看看结果会是什么?
猜你喜欢
  • 2014-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-24
  • 2011-08-25
  • 1970-01-01
  • 2011-11-21
  • 2011-01-31
相关资源
最近更新 更多