I have the following class whereby I am trying to make a database connection to a SQL database that is sitting on my PC:
public class SQLServerTest implements Runnable {
public static void main(
String [ ] args){
new SQLServerTest().run();
}
public void run() {
testMS();
}
public void testMS(){
Connection con = null;
String hostname = "localhost"; // current machine
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
con = DriverManager.getConnection("jdbc:microsoft:sqlserver://"+ hostname +":1433;DatabaseName=esdev;User=test;Password=test");
String tablename = "POI";
String fields = "asset.number, charge.to, hosp.code, item.number";
Statement stmt = con.createStatement();
String query = ("SELECT " + fields + " FROM " + tablename);
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String field = rs.getString(1);
String data = rs.getString(2);
// Do something like System.out.println(mykeyfield);
System.out.println("Field: " + field + " Value: " + data);
}
}
catch (Exception e) {
System.out.println(e.getMessage());
}
finally {
if(con != null){
try { con.close(); }
catch (Exception e) {System.out.println("Error closing connection: " + e.getMessage());
}
}
}
}
}
I am getting the following error that does not have a LINE NUMBER associated with it and I cannot see where my problem is:
[Microsoft][SQLServer 2000 Driver for
JDBC][SQLServer]Incorrect syntax near the keyword 'to'.
Any help or direction would be appreciated. Thank you.