This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
One reason you don't see the line number that causes the problem is that you are catching the exception and only printing out the message. Instead of
you should do
It looks like this is just a test, so this will probably suffice. However, in production code, you will probably let this exception propogate (i.e. declare it in the throws clause of the method) so that anyone that calls a method that connects to the database can handle the error more appropriately.
With that said, the error isn't with the Java, precisely. This type of error message is from the SQL database itself. It is telling you that there is a syntax error in your SQL query. There is no line number associated with the error message because JDBC does not attach any line number information to the SQL command that is being sent. Even if it did, the database probably wouldn't know what to do with the information.
Anyway, tracking down the error is somewhat problematic because you are concatenating Strings to build it. I suggest that you look at the value of the String variable named query since it contains the SQL query that you are trying to execute. Perhaps you can add a System.out.println() call for this purpose. Find the word, "to" in this query and the syntax error is somewhere before that. You may want to copy and paste this query into the interactive SQL front-end for your database. (Sorry, I'm not familiar with SQL Server, so I don't know what it's called.) Doing so might give you some more information about the SQL syntax error. If you need some eyes to look at it, please post the exact contents of this String and we'll take a look.
By the way, this message board is for general beginner questions. I personally would not consider this a "beginner" question. In fact, we have a forum that is dedicated to JDBC. Please post future questions there.