日期:2014-05-17  浏览次数:21169 次

Jsp + Oracle 如何取回id,报错getInt not implemented for class oracle.jdbc.driver.T4CRo
Java code

<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*,bbs.*"%>
<%
    request.setCharacterEncoding("UTF-8");
    String action = request.getParameter("action");
    if (action != null && action.trim().equals("post")) {
        String title = request.getParameter("title");
        //System.out.println(title);
        String cont = request.getParameter("cont");
        //System.out.println(cont);

        Connection conn = DB.getConn();
        boolean autoCommit = conn.getAutoCommit();
        conn.setAutoCommit(false);
        
        String sql = "insert into article values(seq_article.nextval,?,?,?,?,sysdate,?)";
        PreparedStatement pstmt = DB.prepareStmt(conn, sql,Statement.RETURN_GENERATED_KEYS);
   
        pstmt.setInt(1, 0);
        pstmt.setInt(2, 1);
        pstmt.setString(3, title);
        pstmt.setString(4, cont);
        pstmt.setInt(5, 0);
        pstmt.executeUpdate();
        
        ResultSet rsKey = pstmt.getGeneratedKeys();    
        rsKey.next();
    //System.out.print(rsKey);
        int rootId= rsKey.getInt(1);//这句出错
        Statement stmt = DB.createStmt(conn);
        stmt.executeUpdate("update article set rootid=" + rootId + " where id="+ rootId);
        conn.commit();
        conn.setAutoCommit(autoCommit);
        DB.close(stmt);
        DB.close(pstmt);
        DB.close(conn);
        response.sendRedirect("article.jsp");
    }
%>



运行以后会报错:无效的列类型: getInt not implemented for class oracle.jdbc.driver.T4CRowidAccessor
在网上查了半天,说是驱动的问题,可以这样改
  String generatedColumns[] = { "id" };
   
  PreparedStatement pstmt = DB.prepareStmt(sql,generatedColumns);


我的DB封装类里定义的是
public static PreparedStatement prepareStmt(Connection conn,String sql,int autoGeneratedKeys){
PreparedStatement pstmt= null;
try {
pstmt= conn.prepareStatement(sql,autoGeneratedKeys);
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;

}
第二个参数是String类型的,但我写的类里面是int类型的,好像没办法改成String类型的
---------------------------------------------------------
public static PreparedStatement prepareStmt(Connection conn,String sql,String autoGeneratedKeys){
PreparedStatement pstmt= null;
try {
pstmt= conn.prepareStatement(sql,Integer.parseInt(autoGeneratedKeys));
} catch (SQLException e) {
e.printStackTrace();
}
return pstmt;


这样写了个重载函数,然后语句这样写,可还是不对~~~
String generatedColumns[] = { "id" }; 
PreparedStatement pstmt = DB.prepareStmt(conn,sql,generatedColumns[1]);
来个大神,帮帮忙啊!!!~~~~~~

------解决方案--------------------
rsKey.getInt(1);改成rsKey.getLong(1);
这是数据类型的问题,楼主先要确定数据库是什么类型,在决定用什方法
------解决方案--------------------
Java code
int rootId = -1;
if(rsKey.next()){
   rootId= rsKey.getInt(1);
}

------解决方案--------------------
1.你这是什么版本的驱动,class12.jar和ojdbc14.jar都不支持Statement.getGeneratedKeys方法
2.反编译看了一下,T4CRowidAccessor这个类没有getInt方法,你用getString方法试一试
------解决方案--------------------
探讨

引用:

1.你这是什么版本的驱动,class12.jar和ojdbc14.jar都不支持Statement.getGeneratedKeys方法
2.反编译看了一下,T4CRowidAccessor这个类没有getInt方法,你用getString方法试一试

能换个新驱动不?