Ok, I have no hair left. When passing a constant string to executeQuery there's no problem. When passing a String object it fails and the exception is ' incorrect syntax near "."'
.
Everything is essentially exactly the same, but for some reason one case works and the other does not. To give a little background, I am connecting to an SQLServer2008 DB. What gives?
Here's the code that has taken my hair from me:
public List<IdValuePair> Get_Job_Types() {
ArrayList<IdValuePair> jt = Get_Id_Value_Pairs(
"SELECT * FROM USER_JOB_TYPE", "JOB_TYPE_ID", "JOB_TYPE");
return (jt);
}
private List<IdValuePair> Get_Id_Value_Pairs(String query, String idRowName,
String valueRowName) {
try {
List<IdValuePair> pairs = new ArrayList<IdValuePair>();
Class.forName("net.sourceforge.jtds.jdbc.Driver");
m_Connection = DriverManager.getConnection(db_connect_string,
db_userid, db_password);
Statement stmt = m_Connection.createStatement();
// THIS WORKS ?!
ResultSet rs = stmt.executeQuery("SELECT * FROM USER_JOB_TYPE");
// THIS DOESN'T !!!!???
rs = stmt.executeQuery(query);
// ...
rs.close();
stmt.close();
return (pairs);
}
catch (Exception e) {
e.printStackTrace();
return (null);
}
}
When passing a String object it fails and the exeception is ' incorrect syntax near "." '.
What gives?
Based on the evidence that you have presented, the problem is in your query string. Basically, SQLServer is telling you that there is a syntax error in your SQL query.
But this works:
ResultSet rs = stmt.executeQuery("SELECT * FROM USER_JOB_TYPE");
... because the syntax of that SQL query is fine.
Print out the actual value of query
that you are using in:
rs = stmt.executeQuery(query);
If the solution isn't obvious, add the value of query
to your Question so that we can look at it.
On rereading the question, it looks like the query
string should be identical to the one the worked. But the evidence of the exception / message says that it isn't, and I'm more inclined to believe THAT evidence than the source code.