int id=0;
String sql="insert into user_info(user_name) values('hehe');select @@IDENTITY as id";
conn.executeUpdate(sql);  上面错了,哪位能改以下?
最好不用存储过程,有啥方法?

解决方案 »

  1.   

    int id=0;
    String sqlInsert="insert into user_info(user_name) values('hehe')";
    String sqlQuery = "select @@IDENTITY as id";
    conn.executeUpdate(sqlInsert);
    ResultSet rs = conn.executeQuery(sqlQuery); 
    while(rs.next())
    id = rs.getInt(1) ;
      

  2.   

    int id=0;
    String sqlInsert="insert into user_info(user_name) values('hehe')";
    String sqlQuery = "select @@IDENTITY as id";
    conn.executeUpdate(sqlInsert);
    ResultSet rs = conn.executeQuery(sqlQuery); 
    while(rs.next())
    id = rs.getInt(1) ;
    out.print(id);打印出的id=0
      

  3.   

    建议先插入在查找  本来就是两个业务
    kevinliuu(@。@) 兄已经给出答案了
      

  4.   

    Statement stmt = conn.createStatement();int id=0;
    String sql="insert into user_info(user_name) values('hehe');select @@IDENTITY as id";if(stmt.execute(sql)){
     id = stmt.getResultSet().getInt(1);
    }System.out.println(id);
      

  5.   

    public int getNewListNo(List list) throws DBError {
            int i = -1;
            PreparedStatement pstm = null;
            ResultSet rs = null;
            Connection con = null;
            try {
                con = DBsrc.getProxoolConn();
                pstm = con.prepareStatement(
                        "insert into List(peopleID,listDate) values(?,?)",
                        PreparedStatement.RETURN_GENERATED_KEYS);
                pstm.setString(1, list.getPeopleID());
                pstm.setTimestamp(2, new Timestamp(list.getListDate().getTime()));
                if (pstm.executeUpdate() > 0) {
                    rs = pstm.getGeneratedKeys();
                }
                if (rs.next()) {
                    i = rs.getInt(1);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (pstm != null) {
                    try {
                        pstm.close();
                    } catch (SQLException ex) {
                        ex.printStackTrace();
                    }
                }
                if (con != null) {
                    try {
                        con.close();
                    } catch (SQLException ex1) {
                        ex1.printStackTrace();
                    }
                }
            }
            return i;
        }
      

  6.   

    我的忘了加next()了别忘了补上