简体   繁体   English

SQL查询Java抛出异常

[英]SQL query in Java throwing exception

I have some Java pulling from an Access database.我从 Access 数据库中提取了一些 Java。 Here's the code doing the querying:这是执行查询的代码:

Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
String filename = "C:/Development/tomcat/webapps/inquire/inquire.mdb";
String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
database+= filename.trim() + ";DriverID=22;READONLY=true}";
Connection con = DriverManager.getConnection( database ,"","");
Statement s = con.createStatement();
s.execute ("SELECT manager FROM inquiries");
ResultSet rs = s.getResultSet();

Yes, there's a database named inquire.mdb at that location, with a table called 'inquiries' with a column named 'manager'是的,该位置有一个名为inquiry.mdb 的数据库,其中有一个名为“inquiries”的表和一个名为“manager”的列

However, when the code executes I'm getting:但是,当代码执行时,我得到:

java.sql.SQLException: Column not found java.sql.SQLException:找不到列

It's really weird because in another place this query works:这真的很奇怪,因为在另一个地方这个查询有效:

String theQuery = "SELECT DISTINCT manager FROM inquiries";
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        String filename = "C:/Development/tomcat/webapps/inquire/inquire.mdb";
        String database = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=";
        database+= filename.trim() + ";DriverID=22;READONLY=true}";
        Connection con = DriverManager.getConnection( database ,"","");
        Statement s = con.createStatement();
        s.execute(theQuery);
        ResultSet rs = s.getResultSet();

Of course, as is almost always the problem when I run into a programming issue, it was something stupid I did.当然,当我遇到编程问题时几乎总是会出现问题,这是我做的一些愚蠢的事情。

The problem was in the processing of the resultset, not the query.问题在于结果集的处理,而不是查询。 I had simplified the query but was still using other columns in the processing.我已经简化了查询,但仍在处理中使用其他列。 So yeah, of course it couldn't find the column, I wasn't even pulling it with my query.所以,是的,它当然找不到该列,我什至没有用我的查询来拉它。

If I wasn't stupid I would have printed the whole frigging stacktrace, not just the stupid exception.如果我不愚蠢,我会打印整个该死的堆栈跟踪,而不仅仅是愚蠢的异常。

Then I could find what line it was actially dying at.然后我可以找到它实际上死在哪条线上。

Then it takes just a few seconds to fix.然后只需几秒钟即可修复。

Instead of, you know, HOURS.而不是,你知道,HOURS。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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