0

At the moment I have a table that look like this:

ID       VAR1   VAR2
SN453    1      blablabla
SN453    2      blablabla
AK221    1      blablabla
AK221    2      blablabla

What I want to do is to select VAR1 and VAR2 for a given ID. ID is in a String variable, I would use this:

ps = connect.prepareStatement("SELECT VAR1, VAR2 FROM TABLE1 WHERE ID = ?");

How can I tell SQLite to pick the ID value from my string variable?

2 Answers 2

3
ps = connect.prepareStatement("SELECT VAR1, VAR2 FROM TABLE1 WHERE ID =?";
ps.setString(1, myStringVariable); 
           //^ '1' is the index of the ? you're assigning
           //you'd have multiple setters if you had more than one parameter (?)

When using a parameterized query, you need to just assign the variables.

More information can be found in this tutorial

And here is the documentation for the setString(int parameterIndex, String x) method from the PreparedStatement class.

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

Comments

1

You need to bind the desired value to the placeholder ("?") that you put into our SQL statement. Placeholders are referenced by position in the statement, so you need to bind a value to placeholder #1. Something like this would do the trick:

ps.setString(1, id);

Once you've done that, ps.executeQuery() should give you a ResultSet that you can use to extract your results.

See the documentation for java.sql.PreparedStatement for full details.

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.