2013-10-09 91 views
5

我要張貼在Oracle數據庫11g的數據和我輸入使用輸入字段中的數據輸入到數據庫中的數據,但只是ID coulmn和另一個字符串coulmn是得到正確的數據,但另一個coulmn得到空或0,所以我不能解決這個問題,在運行時我有這個錯誤無效的列類型:getInt沒有實現類oracle.jdbc.driver.T4CRowidAccessor

Invalid column type: getInt not implemented for class oracle.jdbc.driver.T4CRowidAccessor

這是我的代碼

public Client newClient(Client client){ 
    try { 
     con = DBConnection.getConnection(driver, url, name, pass);  
     pstmt = con.prepareStatement("INSERT INTO CLIENT (CID, FIRSTNAME, LASTNAME, CAREER, CSALARY) VALUES (CLIENT_ID.nextval, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); 

     pstmt.setString(1, client.getFirstName()); 
     pstmt.setString(2, client.getLastName()); 
     pstmt.setString(3, client.getCareer()); 
     pstmt.setInt(4, client.getcSalary()); 
     pstmt.executeUpdate();  
     rs = pstmt.getGeneratedKeys(); 
     rs.next(); 
     Integer id = rs.getInt(1); 
      client.setcId(id);   
    }catch(Exception ex){ 
     ex.printStackTrace(); 
     return null; 
    }finally{ 
     try{ rs.close(); }catch (Exception e){} 
     try{ pstmt.close();}catch (Exception e){} 
     try{ con.close();}catch (Exception e){} 
    }//finally 
    return client; 
} 

}

@XmlRootElement 
    public class Client { 
private Integer cId; 
private String firstName; 
private String lastName; 
private String career; 
private int cSalary; 
public Integer getcId() { 
    return cId; 
} 
public void setcId(Integer cId) { 
    this.cId = cId; 
} 
public String getFirstName() { 
    return firstName; 
} 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 
public String getLastName() { 
    return lastName; 
} 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 
public String getCareer() { 
    return career; 
} 
public void setCareer(String career) { 
    this.career = career; 
} 
public int getcSalary() { 
    return cSalary; 
} 
public void setcSalary(int cSalary) { 
    this.cSalary = cSalary; 
} 

}

,這是我的SQL

CREATE TABLE "AHMAD"."CLIENT" 
    ("CID" NUMBER(*,0) NOT NULL ENABLE, 
"FIRSTNAME" VARCHAR2(255 BYTE), 
"LASTNAME" VARCHAR2(255 BYTE), 
"CAREER" VARCHAR2(255 BYTE), 
"CSALARY" NUMBER, 
CONSTRAINT "CLIENT_PK" PRIMARY KEY ("CID") 
    USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT  CELL_FLASH_CACHE DEFAULT) 
    TABLESPACE "USERS" ENABLE 
    ) SEGMENT CREATION IMMEDIATE 
    PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING 
     STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645 
     PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE   DEFAULT CELL_FLASH_CACHE DEFAULT) 
     TABLESPACE "USERS" ; 

plz幫助我的數據插入到表,但不是完全地和我有這個運行時錯誤

   java.sql.SQLException: Invalid column type: getInt not implemented for class oracle.jdbc.driver.T4CRowidAccessor 
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) 
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146) 
at oracle.jdbc.driver.Accessor.unimpl(Accessor.java:358) 
at oracle.jdbc.driver.Accessor.getInt(Accessor.java:468) 
at oracle.jdbc.driver.OracleReturnResultSet.getInt(OracleReturnResultSet.java:265) 
at com.rest.client.ClientDAO.newClient(ClientDAO.java:99) 
at com.rest.client.ClientResourcs.newClient(ClientResourcs.java:36) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
at java.lang.reflect.Method.invoke(Unknown Source) 
at  com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.jav a:60) 
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185) 
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) 
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) 
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) 
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) 
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:708) 
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:936) 
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) 

回答

7

的使用Statement.RETURN_GENERATED_KEYS時,Oracle JDBC驅動程序不會返回生成的ID,因此生成的密鑰ResultSet的第一列是不是int,而是返回一個ROWID(見JDBC tutorial on rowids)插入行(這樣你就可以自己查詢行)。據我所知,原因是Oracle沒有身份類型,所以實際上並不知道哪個字段是生成的密鑰。

相反,您應該能夠使用prepareStatement(String sql, int[] columnIndexes)prepareStatement(String sql, String[] columnNames)明確指定要返回的列。

+0

將數據插入表中,並且id是自動增量,但整行像這樣1003 \t null \t null \t Dev \t 0原諒我iam初學者,並有錯誤 – Ahmad

+0

再次閱讀我的答案。問題是,您認爲getGeneratedKeys的第一列包含插入行的「CID」列的值,但它不包含。它包含rowid,它是由Oracle定義的特殊僞列。 –

+1

prepareStatement(SQL字符串,INT [] columnIndexes)工程的Oracle 11g –

16

像馬克的建議,我只是遇到了同樣的問題,找到了解決辦法:

String generatedColumns[] = { "ID" }; 
stmt = con.prepareStatement(query, generatedColumns); 

你執行查詢後,執行以下操作以獲取插入的ID。

rs = stmt.getGeneratedKeys(); 
rs.next(); 
rs.getInt(1); 
+0

謝謝!我有一段時間出現這個錯誤,但從未需要插入後使用任何ID ..但現在我有需要,問題必須解決!注意:爲什麼oracle太過時了! –

1

不知道這個問題是否是原來決心或沒有,但我面臨同樣的問題,並得到了由實體類INT更換整數(在你的情況下,它是客戶類)

解決