SQL在SQL开发人员中工作正常,但是当我尝试在ResultSet中使用它引发Invalid Column Name
错误时,代码:
String assignList = "SELECT table1.name , table1.spads , table1.group , table1.id , max(table2.date_) ,max(table2.event) "
+ " FROM FOMOWNER_LSR.table1 LEFT OUTER JOIN ( SELECT id, MAX(date_) AS latest FROM FOMOWNER_LSR.table2 GROUP BY id ) rev "
+ " ON rev.id = table1.id LEFT OUTER JOIN FOMOWNER_LSR.table2 ON table2.id = rev.id "
+ " AND table2.date_ = rev.latest group by table1.name , table1.spads , table1.group ,table1.id order by table1.id desc " ;
PreparedStatement assignListStmt = null;
Connection conn = null;
ResultSet rs = null;
// StringBuffer body = new StringBuffer();
try {
ArrayList<getssets> getset = new ArrayList<getssets>();
//conn = DriverManager.getConnection("jdbc:oracle:thin:@hw-dbt-rac.syniverse.com:1521/WLNPT", "utlapp100", "g0wlnp");
System.out.println("before calling fomlsr DB connection");
conn = DBConnector.getConnection();
System.out.println("after calling fomlsr DB connection");
if (conn == null)
throw new SQLException("NULL connection in GUIActionClass");
if (conn.isClosed())
throw new SQLException("Connection is closed");
conn.setAutoCommit(false);
System.out.println("DB CONNECTION ESTABLISHED");
assignListStmt = conn.prepareStatement(assignList);
rs = assignListStmt.executeQuery();
System.out.println("After the execute query");
while (rs.next()) {
getssets guiAssign = new getssets();
guiAssign.setName(rs.getString("name"));
guiAssign.setspads(rs.getString("spads"));
guiAssign.setgroup(rs.getString("group"));
guiAssign.setId(rs.getString("id"));
guiAssign.setDate(rs.getTimestamp("date_"));
guiAssign.setEvent(rs.getString("event"));
getset.add(guiAssign);
}
另一个带有setter和getter(getssets)的类文件
private String name;
private String spads;
private String group;
private String id;
private String date_;
private String event;
public String getName() {
return nullToEmpty(name);
}
public void setName(String name) {
this.name = name;
}
public String getspads() {
return nullToEmpty(spads);
}
public void setspads(String spads) {
this.spads = spads;
}
public String getgroup() {
return nullToEmpty(group);
}
public void setgroup(String group) {
this.group = group;
}
public String getId() {
return nullToEmpty(id);
}
public void setId(String id) {
this.id = id;
}
public String getDate() {
return nullToEmpty(date_);
}
public void setDate(String date_) {
this.date_ = date_;
}
public String getEvent() {
return nullToEmpty(event);
}
public void setEvent(String Event) {
this.event = event;
}
private String nullToEmpty(String st){
if(st==null)
st = "";
return st;
我的任务是使用JSP在GUI中打印这些值。
以下是我在日志中遇到的错误
java.sql.SQLException: Invalid column name
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:208)
at oracle.jdbc.driver.OracleStatement.getColumnIndex(OracleStatement.java:3291)
at oracle.jdbc.driver.OracleResultSetImpl.findColumn(OracleResultSetImpl.java:1914)
at oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:1515)
at weblogic.jdbc.wrapper.ResultSet_oracle_jdbc_driver_OracleResultSetImpl.getString(Unknown Source)
at com.tsiconnections.fom.action.GUIActionClass.execute(GUIActionClass.java:83)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1480)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:506)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:184)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3750)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3714)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2283)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2182)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1491)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
<Aug 28, 2018 5:52:15 AM EDT> <Error> <com.tsiconnections.fom.action.GUIActionClass> <BEA-000000> <SQLException: Invalid column name>
com.tsiconnections.fom.exception.FilterServiceException
at com.tsiconnections.fom.action.GUIActionClass.execute(GUIActionClass.java:96)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1480)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:506)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:227)
at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:125)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:301)
at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:184)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.wrapRun(WebAppServletContext.java:3750)
at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3714)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2283)
at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2182)
at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1491)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
<Aug 28, 2018 5:52:15 AM EDT> <Error> <HTTP> <BEA-101020> <[ServletContext@157132757[app:fomWebLSR module:fomWebLSR path:/fomWebLSR spec-version:null]] Servlet failed with Exception
java.lang.NullPointerException
at org.apache.struts.action.RequestProcessor.processForwardConfig(RequestProcessor.java:441)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:279)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1480)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:506)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
Truncated. see log file for complete stacktrace
>
在SQL和Java中可接受名称为“ date_”的列吗? 我可以使用字符串获取日期和时间吗? 还有什么需要验证的?
答案 0 :(得分:2)
您的列名应保持一致,并使用别名更改SQL的最后几列:
max(table2.date_) as date_ ,max(table2.event) as event