Search code examples
sql-serverandroid-studioresultset

Retrieve data return "" in ResultSet. SQL SERVER + ANDROID STUDIO


urlImage always show "", not error and not null

 @Override
    protected String doInBackground(String... strings) {
        try
        {
            Connection conn = connPO.CONN(); //Connection Object
            if (conn == null)
            {
            }
            else {
                // Change below query according to your own database.
                String query = "select * from config";
                Statement stmt = conn.createStatement();
                ResultSet rs = stmt.executeQuery(query);
                if (rs != null)
                {
                        try {
                            urlImage = rs.getString("url"); //--here I try get data
                        } catch (Exception ex) {
                            ex.printStackTrace();
                        }

                    msg = "TEST = "+urlImage;
                } else {
                    msg = "No Data found!";
                }
            }
        } catch (Exception e)
        {
            e.printStackTrace();
            Writer writer = new StringWriter();
            e.printStackTrace(new PrintWriter(writer));
            msg = writer.toString();
        }
            return msg;
    }

E/MSG===>: TEST =

urlImage = rs.getString("url") //-- why this are "" ?

propertiy url there is in database with value.


Solution

  • You need to call rs.next() before trying to get data from the ResultSet. The first call will move the result set cursor to the first row.

    From Java Se:

    boolean next() throws SQLException

    Moves the cursor froward one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.