1

我正在尝试从 servlet 创建一个表。但是当我运行时,它显示错误java.sql.SQLException: ORA-00902: invalid datatype。但是当我在 sql plus 中执行相同的查询时,它正在执行。

堆栈跟踪 :

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

请任何人告诉我我犯了什么错误............谢谢......

4

2 回答 2

2

我猜你在两个语句中都缺少一个结束')',尝试删除'('表名之前的开口。

编辑:

在第二个语句中,您使用了 ID、FROM、TO 等关键字,这些关键字通常会导致冲突。列MESSAGE被定义为默认 null然后不是 null

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)
于 2013-02-11T10:09:54.523 回答
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
)
于 2013-02-11T10:17:50.583 回答