1

I need to know what the number X needs to be in order to receive the data properly.

Example code :

Statement sta = (connection object).createStatement();

sta.executeQuery("SELECT 'points' FROM TABLEX WHERE 'player'='" + player_name + "'").getString(X); ///HERE
1
  • 1
    Even if you get an answer it is not going to work, as you first need to call next() on the ResultSet before you can retrieve a value. Please consult a proper JDBC tutorial and the API documentation. Commented Jul 2, 2014 at 13:18

3 Answers 3

2

Either 1, or "points" will work.

1 is the index of the column as specified in the select statement. The indexing starts from 1 and increments from there.

Otherwise the name of the column may be used, in this case "points". That method may cause a bit more meta data to get loaded and so performance can vary.

Sign up to request clarification or add additional context in comments.

3 Comments

That is not going to work without a call to next(), which is missing in the code of the question.
There is a lot of the boiler plate that is also missing, not just next(). I took it that Zarkopafilis was asking a very specific question about getString. I am happy to update the answer if that is not the case.
Technically upto the getString call everything is fine (assuming (connection object) is meant to leave out connection creation).
2

As the javadoc says :

getString(int columnIndex)

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language. and

getString(String columnLabel)

Retrieves the value of the designated column in the current row of this ResultSet object as a String in the Java programming language.

So this is not going to work. It will give you :

java.sql.SQLException: Before start of result set

First of all you need to iterate through the Resultset obtained using next() then you can retrieve the specific values by either pass 1 which is the column index in this case or points which is the columnName of your table and based on the where clause it shall give you different values of the column points

2 Comments

That is not going to work without a call to next(), which is missing in the code of the question.
Oh yes my mistake updating it.
0

X is the column number(index) in the table.

It is needed to fetch the data from table.

You also can use column name instead of column index like this -

getString(ColumnName);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.