注:该问题不是所有的数据查询都发生,而是对部分数据,提示这样的错误
null
java.sql.SQLException: Illegal operation on empty result set.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:815)
        at com.mysql.jdbc.ResultSetImpl.getStringInternal(ResultSetImpl.java:552
8)
        at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5448)
        at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5488)
        at net.sf.odinms.client.MaplePet.loadFromDb(MaplePet.java:46)
        at net.sf.odinms.tools.MaplePacketCreator.addItemInfo(MaplePacketCreator
.java:1213)
        at net.sf.odinms.tools.MaplePacketCreator.addItemInfo(MaplePacketCreator
.java:1144)
        at net.sf.odinms.tools.MaplePacketCreator.getCharInfo(MaplePacketCreator
.java:866)
        at net.sf.odinms.net.channel.handler.PlayerLoggedinHandler.handlePacket(
PlayerLoggedinHandler.java:94)
        at net.sf.odinms.net.MapleServerHandler.messageReceived(MapleServerHandl
er.java:118)
        at org.apache.mina.common.support.AbstractIoFilterChain$TailFilter.messa
geReceived(AbstractIoFilterChain.java:570)
        at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageR
eceived(AbstractIoFilterChain.java:299)
        at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(Abst
ractIoFilterChain.java:53)
        at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.mess
ageReceived(AbstractIoFilterChain.java:648)
        at org.apache.mina.filter.codec.support.SimpleProtocolDecoderOutput.flus
h(SimpleProtocolDecoderOutput.java:58)
        at org.apache.mina.filter.codec.ProtocolCodecFilter.messageReceived(Prot
ocolCodecFilter.java:180)
        at org.apache.mina.common.support.AbstractIoFilterChain.callNextMessageR
eceived(AbstractIoFilterChain.java:299)
        at org.apache.mina.common.support.AbstractIoFilterChain.access$1100(Abst
ractIoFilterChain.java:53)
        at org.apache.mina.common.support.AbstractIoFilterChain$EntryImpl$1.mess
ageReceived(AbstractIoFilterChain.java:648)
        at org.apache.mina.filter.executor.ExecutorFilter.processEvent(ExecutorF
ilter.java:220)
        at org.apache.mina.filter.executor.ExecutorFilter$ProcessEventsRunnable.
run(ExecutorFilter.java:264)
        at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source
)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
        at org.apache.mina.util.NamePreservingRunnable.run(NamePreservingRunnabl
e.java:51)
        at java.lang.Thread.run(Unknown Source)

解决方案 »

  1.   

    问题依旧,对应的源码如下:
    源程序对应: 
      public static MaplePet loadFromDb(int itemid, byte position, int petid) { 
            try { 
                MaplePet ret = new MaplePet(itemid, position, petid); 
                Connection con = DatabaseConnection.getConnection(); // Get a connection to the database 
                PreparedStatement ps = con.prepareStatement("SELECT * FROM pets WHERE petid = ?"); // Get pet details.. 
                ps.setInt(1, petid); 
                ResultSet rs = ps.executeQuery(); 
                if(rs.next())
                { 
                ret.setName(rs.getString("name")); 
                ret.setCloseness(rs.getInt("closeness")); 
                ret.setLevel(rs.getInt("level")); 
                ret.setFullness(rs.getInt("fullness")); 
                } 
                rs.close(); 
                ps.close(); 
                return ret; 
            } catch (SQLException ex) { 
                Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex); 
                return null; 
            } 
        }
      

  2.   

    public static MaplePet loadFromDb(int itemid, byte position, int petid) { 
            try { 
                MaplePet ret = new MaplePet(itemid, position, petid); 
                Connection con = DatabaseConnection.getConnection(); // Get a connection to the database 
                PreparedStatement ps = con.prepareStatement("SELECT * FROM pets WHERE petid = ?"); // Get pet details.. 
                ps.setInt(1, petid); 
                ResultSet rs = ps.executeQuery(); 
                if(rs.next()) 
                { 
                ret.setName(rs.getString("name")); 
                ret.setCloseness(rs.getInt("closeness")); 
                ret.setLevel(rs.getInt("level")); 
                ret.setFullness(rs.getInt("fullness")); 
                } 
                return ret; 
            } catch (SQLException ex) { 
                Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex); 
                return null; 
            } 
    finally{
    try{
    rs.close(); 
                ps.close(); 
    con.close();}catch (SQLException e)
    {
    }
        }
      

  3.   

    if(rs.next()); 
            { 
            ret.setName(rs.getString("name")); 
            ret.setCloseness(rs.getInt("closeness")); 
            ret.setLevel(rs.getInt("level")); 
            ret.setFullness(rs.getInt("fullness")); 
            } else{ 
             return null; 
            }         return ret; 
        } catch (SQLException ex) { 
            Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex); 
            return null; 
        }finally{ 
                  rs.close(); 
                  ps.close(); 
        } 
      

  4.   


    根据以上修改后,编译器会提示错误如下:
                  rs.close(); 
                ps.close(); 
    找不到符号,符号变量rs 及 ps
      

  5.   

    ...老兄,遇到问题先思考一下了嘛
    在try外面定义方法变量ResultSet rs =null;PreparedStatement ps=null;这个错误是变量作用域的范畴
      

  6.   

    ret.setCloseness(rs.getInt("closeness")); 
    ret.setLevel(rs.getInt("level")); 
    ret.setFullness(rs.getInt("fullness")); 你看看你的数据库的列值吧,可能是null的,转为int估计就出错了
      

  7.   

    。。
    8楼的方式你先检查一下然后你的代码:
    public static MaplePet loadFromDb(int itemid, byte position, int petid) { 
    PreparedStatement ps =null;
    ResultSet rs = null;
            try { 
                MaplePet ret = new MaplePet(itemid, position, petid); 
                Connection con = DatabaseConnection.getConnection(); // Get a connection to the database 
                ps = con.prepareStatement("SELECT * FROM pets WHERE petid = ?"); // Get pet details.. 
                ps.setInt(1, petid); 
                rs = ps.executeQuery(); 
                if(rs.next()); 
                { 
                ret.setName(rs.getString("name")); 
                ret.setCloseness(rs.getInt("closeness")); 
                ret.setLevel(rs.getInt("level")); 
                ret.setFullness(rs.getInt("fullness")); 
                } else{ 
                    return null; 
                }             return ret; 
            } catch (SQLException ex) { 
                Logger.getLogger(MaplePet.class.getName()).log(Level.SEVERE, null, ex); 
                return null; 
            }finally{ 
                      rs.close(); 
                      ps.close(); 
            } 
        }
      

  8.   

    ret.setCloseness(rs.getInt("closeness")); 
    ret.setLevel(rs.getInt("level")); 
    ret.setFullness(rs.getInt("fullness")); 
    看看是不是数据库里值为null,或列名写错了
      

  9.   


    定义后,编译器提示,try里面那2个已被定义过的错误
      

  10.   


    SELECT * FROM pets WHERE petid = ?
    这个petid是唯一的吗??
      

  11.   

    解决方法是:可以使用Integer row="select count(*) from hpibbs.firstcard where firstcard_id="+allcard_id;这时候,总会有返回的行,即使没有符合条件的数据
      

  12.   

     ps = con.prepareStatement("SELECT * FROM pets WHERE petid = ?"); 
     ps.setInt(1, petid); 
    好像是ps.setInt(0, petid);
      

  13.   


    后经反复测试和输出数据对比,发现错误的那个数据里面的petid=208,在数据库中没有208的这条数据,可我的数据库中所有的petid=0,怎么能出来208的数据呢,不知道哪里来的,真是晕啊!