Skip to content
Advertisement

For JDBC prepared statements, how are the position of variables determined when an actual value is supplied in the SQL statement

If I have the following code, with one value supplied in the SQL statement, what would be the value of the position for GROUP? Is it 2 or 3? In other words, is the supplied value counted when determining the positions?

PreparedStatement st = null;
Connection conn      = DBConnection.getConnection();
String userId        = "myuser";
String group         = "mygroups";

String sql = "Insert into MY_TABLE (USER_ID, LOGIN_DATE, GROUP) values (?,SYSDATE,?)";

st = conn.prepareStatement(sql);
st.setString(1, userId);
st.setString(2, group);

Advertisement

Answer

The indexes passed to the setXYZ methods refer to the number of placeholders – 1 is the first placeholder, 2 is the second, etc., regardless of where exactly they are used in the statement and what other literal parts that clause happens to have.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement