简体   繁体   中英

MSSQL - Invalid column name with Java/JTDS

I have read all the threads about my problem and all of them finish without solution... Anyway I hope to be luckier...

I'm trying to use JDBC to connect to the MSSQL db. I used the driver from Microsoft sqljdbc4.jar and I had no problem. Since I have to switch to JTDS (I'm using 1.2.8) I got this exception when I try to run the same SQL string:

java.sql.SQLException: Invalid column name 'id'.    at
net.sourceforge.jtds.jdbc.SQLDiagnostic.addDiagnostic(SQLDiagnostic.java:372)
    at net.sourceforge.jtds.jdbc.TdsCore.tdsErrorToken(TdsCore.java:2894)
    at net.sourceforge.jtds.jdbc.TdsCore.nextToken(TdsCore.java:2334)   at
net.sourceforge.jtds.jdbc.TdsCore.getMoreResults(TdsCore.java:643)  at
net.sourceforge.jtds.jdbc.JtdsStatement.executeSQLQuery(JtdsStatement.java:506)
    at
net.sourceforge.jtds.jdbc.JtdsStatement.executeQuery(JtdsStatement.java:1433)

The string is

SELECT tabledatastoreitem.name, tabledatastoreitem.path
FROM tabledatastoreitem, store, service, serviceprovider
WHERE tabledatastoreitem.store_id = store.id
    AND store.service_store_id = service.id
    AND service.serviceprovider_id = serviceprovider.id
    AND serviceprovider.name = 'SystemIService'
    AND store.createdttm < CONVERT(varchar(100), DATEADD(MI, -7, GETDATE()));

Any idea?

From what I can see in your statement, there are 3 possibilities.

store.id
service.id
serviceprovider.id

Debug this by removing portions of the statement, or executing it in the command line, until you figure out which reference to .id is causing you issue. I would highly suggest using naming such as

store.store_id
service.service_id
serviceprovider.serviceprovider_id

It makes debugging and readability a lot easier for you in the future.

Edit:

Try using the following statement. It does the same as yours (from what I can tell), but uses JOINS instead of CROSSES . It could make your debugging a bit easier.

SELECT tabledatastoreitem.name, tabledatastoreitem.path
FROM tabledatastoreitem
JOIN store ON store.id=tabledatastoreitem.store_id
JOIN service ON service.id = store.service_store_id
JOIN serviceprovider ON service.serviceprovider_id = serviceprovider.id
WHERE serviceprovider.name = 'SystemIService'
  AND store.createdttm < CONVERT(varchar(100), DATEADD(MI, -7, GETDATE()));

I'm not sure how this could happen, but you could try escaping all instances of 'id' in the code to see if that fixes it:

SELECT tabledatastoreitem.name, tabledatastoreitem.path
FROM tabledatastoreitem, store, service, serviceprovider
WHERE tabledatastoreitem.store_id = store.[id]
    AND store.service_store_id = service.[id]
    AND service.serviceprovider_id = serviceprovider.[id]
    AND serviceprovider.name = 'SystemIService'
    AND store.createdttm < CONVERT(varchar(100), DATEADD(MI, -7, GETDATE()));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM