嗨,有一个方法可以创建compMac对象的LinkedList。 CompMac对象包含来自mysql表行的元素。每行包含一个昵称和位置,因此我使用循环为每行创建一个“ compMac”并将其添加到链表中。我不知道为什么,但是创建了第一个'compMac'并将其添加到链表中,但是之后我得到了

java.sql.SQLException: Column 'nickname' not found.
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1078)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:975)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:920)
at com.mysql.jdbc.ResultSetImpl.findColumn(ResultSetImpl.java:1163)
at com.mysql.jdbc.ResultSetImpl.getString(ResultSetImpl.java:5729)
at server.DbConnect.getArray(DbConnect.java:224)
at server.doComms.sendArray(doComms.java:127)
at server.doComms.whileChatting(doComms.java:107)
at server.doComms.run(doComms.java:32)
at java.lang.Thread.run(Thread.java:695)


这是我的方法:

public LinkedList<compMac> getArray()
{
    LinkedList<compMac> locArray = new LinkedList<compMac>();
    try{
        String query = "SELECT * FROM online";
        rs = st.executeQuery(query);
        while(rs.next()){
            String nn = rs.getString("nickname");
            String loc = rs.getString("location");//mac addresses
            String cX=getCoorX(loc);
            String cY=getCoorY(loc);
            String building=getBuilding(loc);
            String floor=getFloor(loc);

            locArray.add(new compMac(nn,loc,cX,cY,building,floor));

            }
    }
    catch(Exception e){e.printStackTrace();}
    return locArray;

}


但是我确实有一个昵称列,就像我说的那样,创建了第一个compMac,当我弹出链表时,我得到了第一行的信息。

感谢任何人知道我的代码有什么问题。

编辑:这里的类代码:
      包服务器;
      导入java.sql。*;
      导入java.util.LinkedList;

  import com.example.client.Mac;
  import com.example.client.compMac;


  public class DbConnect {

private Connection con;
private Statement st;
private Statement stClean;
private ResultSet rs;

public DbConnect(){

    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql://melucio.no-ip.biz:3306/TOC","XXXXX","XXXXXXX");
        st = con.createStatement();
        stClean = con.createStatement();

    }catch(Exception ex){
        System.out.println("Error: "+ex);
    }
}






/*This method returns the x coor. of mac address*/
    public String getCoorX(String mac){
    String cX = "";
    try{
        String query = "SELECT * FROM coordinate";
        rs = st.executeQuery(query);
        while(rs.next()){
            if(rs.getString("mac").equals(mac)){
                return rs.getString("coorX");
            }
        }
    }
    catch(Exception e){}
    return cX;

}

    /*This method returns the y coor. of mac address*/
    public String getCoorY(String mac){
    String cY = "";
    try{
        String query = "SELECT * FROM coordinate";
        rs = st.executeQuery(query);
        while(rs.next()){
            if(rs.getString("mac").equals(mac)){
                return rs.getString("coorY");
            }
        }
    }
    catch(Exception e){}
    return cY;

}   public String getBuilding(String mac){
        String buil = "";
        try{
            String query = "SELECT * FROM coordinate";
            rs = st.executeQuery(query);
            while(rs.next()){
                if(rs.getString("mac").equals(mac)){
                    return rs.getString("building");
                }
            }
        }
        catch(Exception e){}
        return buil;

    }

    public String getFloor(String mac){
        String fl = "";
        try{
            String query = "SELECT * FROM coordinate";
            rs = st.executeQuery(query);
            while(rs.next()){
                if(rs.getString("mac").equals(mac)){
                    return rs.getString("floor");
                }
            }
        }
        catch(Exception e){}
        return fl;

    }



/*Update the location table*/
public void updateLocation(String userNN,String location){//,long time){

    long time = System.currentTimeMillis();
    if(!isOnline(userNN))
    {System.out.println("not online");
        setOnline(userNN,location);}
    else{
        try{
            //String query ="UPDATE online SET location = '" +location +"' WHERE nickname='" +userNN +"'";
            String query ="UPDATE online SET location = '" +location +"', lastSeen = "+time +"  WHERE nickname='" +userNN +"'";

            st.executeUpdate(query);

        }
        catch(Exception e){System.out.println("ayayaya");}
    }
}


public LinkedList<compMac> getArray()
{
    LinkedList<compMac> locArray = new LinkedList<compMac>();
    try{
        String query = "SELECT * FROM online";
        rs = st.executeQuery(query);
        while(rs.next()){
            String nn = rs.getString("nickname");
            String loc = rs.getString("location");//mac addresses
            String cX=getCoorX(loc);
            String cY=getCoorY(loc);
            String building=getBuilding(loc);
            String floor=getFloor(loc);

            locArray.add(new compMac(nn,loc,cX,cY,building,floor));

            }
    }
    catch(Exception e){e.printStackTrace();}
    return locArray;

}

public void clean()
{
    long currentTime = System.currentTimeMillis();
    long acceptableTime = currentTime-120000;
    try{
        String query = "SELECT * FROM online";
        rs = st.executeQuery(query);
        while(rs.next()){
            String nn = rs.getString("nickname");
            Long ls = rs.getLong("lastSeen");
            if(ls<acceptableTime)//{}
            {stClean.executeUpdate("DELETE FROM online WHERE     nickname='"+nn+"'");}

        }
    }
    catch(Exception e){e.printStackTrace();}

}


}

最佳答案

根据您的代码,您将以不同的方法重用同一Statement对象。因此,在第一次迭代之后,最后一个方法getFloor更改了st对象以在坐标表上进行查询。我建议您在每个方法中初始化单独的Statement对象,并在finally块中将其关闭。

   import com.example.client.Mac;
  import com.example.client.compMac;


  public class DbConnect {

private Connection con;

public DbConnect(){

    try{
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        con = DriverManager.getConnection("jdbc:mysql://melucio.no-ip.biz:3306/TOC","XXXXX","XXXXXXX");


    }catch(Exception ex){
        System.out.println("Error: "+ex);
    }
}






/*This method returns the x coor. of mac address*/
    public String getCoorX(String mac){
    String cX = "";
    try{
        String query = "SELECT * FROM coordinate";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        while(rs.next()){
            if(rs.getString("mac").equals(mac)){
                return rs.getString("coorX");
            }
        }
        rs.close();
        st.close();
    }
    catch(Exception e){}

    return cX;

}

    /*This method returns the y coor. of mac address*/
    public String getCoorY(String mac){
    String cY = "";
    try{
        String query = "SELECT * FROM coordinate";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        while(rs.next()){
            if(rs.getString("mac").equals(mac)){
                return rs.getString("coorY");
            }
        }
        rs.close();
        st.close();

    }
    catch(Exception e){}
    return cY;

}   public String getBuilding(String mac){
        String buil = "";
        try{
            String query = "SELECT * FROM coordinate";
            Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
            while(rs.next()){
                if(rs.getString("mac").equals(mac)){
                    return rs.getString("building");
                }
            }
        rs.close();
        st.close();

        }
        catch(Exception e){}
        return buil;

    }

    public String getFloor(String mac){
        String fl = "";
        try{
            String query = "SELECT * FROM coordinate";
            Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
            while(rs.next()){
                if(rs.getString("mac").equals(mac)){
                    return rs.getString("floor");
                }
            }
        rs.close();
        st.close();
      }
        catch(Exception e){}
        return fl;

    }



/*Update the location table*/
public void updateLocation(String userNN,String location){//,long time){

    long time = System.currentTimeMillis();
    if(!isOnline(userNN))
    {System.out.println("not online");
        setOnline(userNN,location);}
    else{
        try{
            //String query ="UPDATE online SET location = '" +location +"' WHERE nickname='" +userNN +"'";
            String query ="UPDATE online SET location = '" +location +"', lastSeen = "+time +"  WHERE nickname='" +userNN +"'";

            st.executeUpdate(query);

        }
        catch(Exception e){System.out.println("ayayaya");}
    }
}


public LinkedList<compMac> getArray()
{
    LinkedList<compMac> locArray = new LinkedList<compMac>();
    try{
        String query = "SELECT * FROM online";
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        while(rs.next()){
            String nn = rs.getString("nickname");
            String loc = rs.getString("location");//mac addresses
            String cX=getCoorX(loc);
            String cY=getCoorY(loc);
            String building=getBuilding(loc);
            String floor=getFloor(loc);

            locArray.add(new compMac(nn,loc,cX,cY,building,floor));

            }
        rs.close();
        st.close();
  }
    catch(Exception e){e.printStackTrace();}
    return locArray;

}

public void clean()
{
    long currentTime = System.currentTimeMillis();
    long acceptableTime = currentTime-120000;
    try{
        String query = "SELECT * FROM online";
        Statement st = con.createStatement();
       ResultSet rs = st.executeQuery(query);
        while(rs.next()){
            String nn = rs.getString("nickname");
            Long ls = rs.getLong("lastSeen");
            if(ls<acceptableTime)//{}
            {stClean.executeUpdate("DELETE FROM online WHERE     nickname='"+nn+"'");}

        }
    }
    catch(Exception e){e.printStackTrace();}

}

09-04 11:32