我正在尝试从servlet创建一个表。但是我跑了,它显示错误java.sql.SQLException: ORA-00902: invalid datatype
。但是当我在sql中执行相同的查询时,它正在执行。
堆栈跟踪:
java.sql.SQLException: ORA-00902: invalid datatype
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:439)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:395)
at oracle.jdbc.driver.T4C8Oall.processError(T4C8Oall.java:802)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:436)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:194)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:1000)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1307)
at oracle.jdbc.driver.OracleStatement.executeUpdateInternal(OracleStatement.java:1814)
at oracle.jdbc.driver.OracleStatement.executeUpdate(OracleStatement.java:1779)
at oracle.jdbc.driver.OracleStatementWrapper.executeUpdate(OracleStatementWrapper.java:277)
at skypark.CreateUserAc.doGet(CreateUserAc.java:126)
at skypark.CreateUserAc.doPost(CreateUserAc.java:345)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339)
at skypark.Registration.doPost(Registration.java:137)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:931)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我的代码是:
Statement stC=con.createStatement();
StringBuilder sbC=new StringBuilder(1024);
sbC.append("create table ").append(uname).append("COMMENTS(UNAME VARCHAR2(50),COMMENTS VARCHAR2(1100),ITEMID NUMBER)");
String QueryC=sbC.toString();
stC.executeUpdate(QueryC); //stack trace showing error
Statement stCH=con.createStatement();
StringBuilder sbCH=new StringBuilder(1024);
sbCH.append("create table ").append(uname).append("CHAT(ID NUMBER NOT NULL,FROM VARCHAR2(50),TO VARCHAR2(50),MESSAGE VARCHAR2(500) DEFAULT NULL NOT NULL,SENT DATE NOT NULL, RECD NUMBER)");
String QueryCH=sbCH.toString();
stCH.executeUpdate(QueryCH); //stack trace showing error
请有人告诉我我犯了什么错误.........谢谢.......
答案 0 :(得分:2)
我猜你错过了两个语句中的结束')'
,尝试删除表名前的开头'('
。
编辑:
在第二个声明中,您使用的是ID,FROM,TO等,这些通常是可能导致冲突的关键字。 MESSAGE 列定义为默认为空,然后非空:
create table (CHAT(ID NUMBER NOT NULL,
FROM VARCHAR2(50),
TO VARCHAR2(50),
MESSAGE VARCHAR2(500) DEFAULT NULL NOT NULL,
SENT DATE NOT NULL, RECD NUMBER)
答案 1 :(得分:1)
看起来您已成为某处剪切错误的受害者;
sbC.append("create table ").append(uname)
.append("(COMMENTS(UNAME VARCHAR2(50),COMMENTS VARCHAR2(1100),ITEMID NUMBER)");
...将导致类似于;
的SQL语句create table uname_table (
COMMENTS(UNAME VARCHAR(50), <-- strange indeed, cut'n'paste?
COMMENTS VARCHAR2(1100),
ITEMID NUMBER
)
更合理的是;
sbC.append("create table ").append(uname)
.append("(UNAME VARCHAR2(50),COMMENTS VARCHAR2(1100),ITEMID NUMBER)");
......会给予;
create table uname_table (
UNAME VARCHAR(50),
COMMENTS VARCHAR2(1100),
ITEMID NUMBER
)