Question: So I would like to do a select from an SQL database, using Java, subject to a conditional statement (less than or equal to something) subject to some loop in Java. In other words, something like the following:

```
for (int i=0; i< 195; i++) {
            // Get the results of the SQL query
            resultSet = statement.executeQuery( 
                    "SELECT max( millisFromMid ) FROM stockInfo1 WHERE ( millisFromMid <=  34200000 + (120000)*i ) GROUP BY stockID" 
                    );

```

Now Java is returning an exception here, because it doesn't want me to use "i" in this condition; however, without being able to use it here, I'm not sure how I can change this condition. The problem is this: I'm looking to retrieve data from one database, with the intent of doing some manipulation and putting the results into a new database. These manipulations depend on taking the most recent data available, which is why I'm wanting to increase the number bounding millisFromMid. Does that make sense?

Does anyone have a suggestion on how someone might do something like this? This seems like a fundamental skill to have when using Java and SQL together, so I'd very much like to know it.

Answer: 
The problem os that the database is not told about the value of i. Instead make Java put the value into the string and then submit it to the database.

string sql = "select \* from foo where bar=" + i;

You need to remember at all times that the database only knows what you explicitly tell it, and that you are essentially writing code at runtime that will execute on another host.
You can generate an sql string and call it via jdbc, using bind variable is more correct from performance, security. It also saves you from escaping any special characters.

Since you are learning about using sql with jdbc, please read about bind variables. An example is [here](http://steveracanovic.blogspot.in/2008/03/performance-using-bind-variables-in.html)