用select语句查询一次并按ID排序,取最后那条记录的值,你上面语句里面根本没有ID字段,它当然找不到ID字段了。

解决方案 »

  1.   

    必须用select再查询一下吗?ResultSet 类里面没有返回指定字段得方法吗?
    另外我发现即使取得是rs.getString("CompanyID")字段,也返回类似错误~~~~~
    是不是getString这个方法只能用于String Sql 是“select * from vaaa”类型得数据库操作得啊?
      

  2.   

    ResultSet是一个结果集,实际上"insert"操作返回的是一个空的ResultSet
    实际上这类操作是应该用 executeUpdate(),它返回的是整型。你可以用它来判断操作是否成功。
      

  3.   

    先取id,再插入,然后成功返回id
      

  4.   

    是先取id、手动分配id后插入、再返回id好~~~~~~`
    还是先插入,让数据库自动分配id,插入成功后再取id好呢??
      

  5.   

    插入记录是不用插入id的值因为它是自动分配的。
    如下:
    String Sql = "insert into company_modulelink (CompanyID,ModuleID,IsEnabled ) values ('1','1','1')";
    int i = stat.executeUpdate(sql);
    if(i>0){}ResultSet rs=stat.executeQuery(Sql);
    this.company_id = rs.getString("ID")
      

  6.   

    上面的不对,点错按钮了。看下面的
    插入记录是不用插入id的值因为它是自动分配的。
    如下:
    String Sql = "insert into company_modulelink (CompanyID,ModuleID,IsEnabled ) values ('1','1','1')";
    int i = stat.executeUpdate(sql);
    if(i>0){    sql = "select id from company_modulelink order by id desc";
        ResultSet rs=stat.executeQuery(Sql);
        rs.first();
        this.company_id = rs.getString("ID")
    }
      

  7.   

    加入rs.first();后显示
    java.lang.AbstractMethodError: org.gjt.mm.mysql.ResultSet.first()Z
    at company.AddCompanyInfo.AddCompanyInfoFunction(AddCompanyInfo.java:64)
    at _manage._company._update__jsp._jspService(/agehouse/manage/company/update.jsp:35)
    at com.caucho.jsp.JavaPage.service(JavaPage.java:75)
    at com.caucho.jsp.Page.subservice(Page.java:497)
    at com.caucho.server.http.FilterChainPage.doFilter(FilterChainPage.java:182)
    at com.caucho.server.http.Invocation.service(Invocation.java:312)
    at com.caucho.server.http.CacheInvocation.service(CacheInvocation.java:135)
    at com.caucho.server.http.HttpRequest.handleRequest(HttpRequest.java:244)
    at com.caucho.server.http.HttpRequest.handleConnection(HttpRequest.java:163)
    at com.caucho.server.TcpConnection.run(TcpConnection.java:137)
    at java.lang.Thread.run(Thread.java:536)什么问题?为什么加入rs.next();就不报这个错误??而加rs.first()或rs.last();就报错?
      

  8.   

    rs.next();就不报这个错误??
    rs.first()或rs.last();主要应用于回滚式的结果集合更新操作过程中,如果没有设置相关结果集合属性,当然要报错了。
    具体可以查看API帮助上的结果集合属性说明
      

  9.   

    应该采用JDBC2.0的方式创建statment或者preparestatement。同时
    1.只有JDK1.4可以表示是否从插入中返回自动产生的值,但和具体数据库有关系,不易跨平台。
    2.可以用时间(System.currenttime)等做主键,由程序生成(重复的可能性很小)。
      

  10.   

    关于取得ID的方法有很多,在Oracle中有序列,可以直接来用,对于Mysql、SQL Server这样的数据库建议不要用他们的auto_increment类型,第一种方法可以把ID设为Key,先取max(ID)+1,然后将这个值插入,如果差不进去,再select max(ID)+1插入,直到插入为止,这种方法的效率我认为不是很高,而且如果数据库出现异常可能会落入死循环,所以还要去控制!
    第二个方法是Jive中的方法,看下面的代码
    /**
     * $RCSfile: SequenceManager.java,v $
     * $Revision: 1.1.1.1 $
     * $Date: 2002/09/09 13:51:02 $
     *
     * New Jive  from Jdon.com.
     *
     * This software is the proprietary information of CoolServlets, Inc.
     * Use is subject to license terms.
     */package com.jivesoftware.forum.database;import java.sql.*;
    import com.jivesoftware.util.StringUtils;/**
     * Manages sequences of unique ID's that get stored in the database. Database
     * support for sequences varies widely; some don't support them at all. So,
     * we handle unique ID generation with a combination VM/database solution.<p>
     *
     * A special table in the database doles out blocks of unique ID's to each
     * virtual machine that interacts with Jive. This has the following consequences:
     * <ul>
     *  <li>There is no need to go to the database every time we want a new unique
     *      id.
     *  <li>Multiple app servers can interact with the same db without consequences
     *      in terms of id's.
     *  <li>The order of unique id's may not correspond to creation date
     *      (as they once did).
     * </ul>
     */
    public class SequenceManager {    private static final String LOAD_ID =
            "SELECT id FROM jiveID WHERE idType=?";
        private static final String UPDATE_ID =
            "UPDATE jiveID SET id=? WHERE idType=? AND id=?";    /**
         * The number of ID's to checkout at a time. 15 should provide a good
         * balance between speed and not wasing too many ID's on appserver restarts.
         * Feel free to change this number if you believe your Jive setup warrants
         * it.
         */
        private static final int INCREMENT = 15;    // Statically startup a sequence manager for each of the five sequence
        // counters.
        private static SequenceManager[] managers;
        static {
            managers = new SequenceManager[5];
            for (int i=0; i<managers.length; i++) {
                managers[i] = new SequenceManager(i);
            }
        }    /**
         * Returns the next ID of the specified type.
         *
         * @param type the type of unique ID.
         * @return the next unique ID of the specified type.
         */
        public static long nextID(int type) {
            return managers[type].nextUniqueID();
        }    public static long nextID() {
            return managers[0].nextUniqueID();
        }    private int type;
        private long currentID;
        private long maxID;    /**
         * Creates a new DbSequenceManager.
         */
        public SequenceManager(int type) {
            this.type = type;
            currentID = 0l;
            maxID = 0l;
        }    /**
         * Returns the next available unique ID. Essentially this provides for the
         * functionality of an auto-increment database field.
         */
        public synchronized long nextUniqueID() {
            if (! (currentID < maxID)) {
                // Get next block -- make 5 attempts at maximum.
                getNextBlock(5);
            }
            long id = currentID;
            currentID++;
            return id;
        }    /**
         * Performs a lookup to get the next availabe ID block. The algorithm is as
         * follows:<ol>
         *  <li> Select currentID from appropriate db row.
         *  <li> Increment id returned from db.
         *  <li> Update db row with new id where id=old_id.
         *  <li> If update fails another process checked out the block first; go
         *          back to step 1. Otherwise, done.
         * </ol>
         */
        private void getNextBlock(int count) {
            if (count == 0) {
                System.err.println("Failed at last attempt to obtain an ID, aborting...");
                return;
            }
            boolean success = false;
            Connection con = null;
            PreparedStatement pstmt = null;
            try {
                con = ConnectionManager.getConnection();
                // Get the current ID from the database.
                pstmt = con.prepareStatement(LOAD_ID);
                pstmt.setInt(1, type);
                ResultSet rs = pstmt.executeQuery();
                if (!rs.next()) {
                    throw new SQLException("Loading the current ID failed. The " +
                        "jiveID table may not be correctly populated.");
                }
                long currentID = rs.getLong(1);
                pstmt.close();
                // Increment the id to define our block.
                long newID = currentID + INCREMENT;
                // The WHERE clause includes the last value of the id. This ensures
                // that an update will occur only if nobody else has performed an
                // update first.
                pstmt = con.prepareStatement(UPDATE_ID);
                pstmt.setLong(1, newID);
                pstmt.setInt(2, type);
                pstmt.setLong(3, currentID);
                // Check to see if the row was affected. If not, some other process
                // already changed the original id that we read. Therefore, this
                // round failed and we'll have to try again.
                success = pstmt.executeUpdate() == 1;
                if (success) {
                    this.currentID = currentID;
                    this.maxID = newID;
                }
            }
            catch( Exception sqle ) {
                sqle.printStackTrace();
            }
            finally {
                try {  pstmt.close();   }
                catch (Exception e) { e.printStackTrace(); }
                try {  con.close();   }
                catch (Exception e) { e.printStackTrace(); }
            }
            if (!success) {
                System.err.println("WARNING: failed to obtain next ID block due to " +
                    "thread contention. Trying again...");
                // Call this method again, but sleep briefly to try to avoid thread
                // contention.
                try {
                    Thread.currentThread().sleep(75);
                } catch (InterruptedException ie) { }
                getNextBlock(count-1);
            }
        }
    }这是用了一个多态的方法,来解决取ID的问题,很不错!