SQLException错误表示现有列不存在

时间:2015-05-03 23:06:02

标签: java derby sqlexception

运行searchQuery时,我收到一条SQLException错误,指出找不到我的某个列。这非常令人困惑,因为我在代码的前面引用了该列,并且程序可以在该点找到该列。我的语法有问题吗?谢谢!

public void createDB(){    
    String createCharTableSQL = "CREATE TABLE CosplayCharacter (" +
                "CharacterID int NOT NULL primary key GENERATED ALWAYS " +
                "AS IDENTITY (START WITH 1, INCREMENT BY 1), " +
                "name varchar(60) not null," +
                "gender varchar(10)," +
                "genreID int, " +
                "universeID int, " +
                "mediaID int, " +
                "description varchar(60))";

      statement.executeUpdate(createCharTableSQL);
}

public void insertCharacter(){    
  String fetchAllDataSQL = "SELECT * from CosplayCharacter";


    //Code can find the universeID column here
        resultSet = statement.executeQuery(fetchAllDataSQL);
        while (resultSet.next()) {
            String name = resultSet.getString("name");
            int universeCharID = resultSet.getInt("universeID");

            System.out.println("Character Name : " + name +
                    " UniverseID : " + universeCharID);
        }
}

//But returns error here
public void searchCharacter(String characterName){
    String fetchAllDataSQL = "SELECT * from CosplayCharacter where name = '" + characterName +"'";

        resultSet = statement.executeQuery(fetchAllDataSQL);
        while (resultSet.next()) {
            String gender = resultSet.getString("gender");
            int genreID = resultSet.getInt("genreID");
//Method to search a column in a joined table; no error
            String genreName = getGenreName(genreID);
            int universeID = resultSet.getInt("universeID");
   }
}

堆栈追踪:

 java.sql.SQLException: Column 'universeID' not found.
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown  
Source)
at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
at org.apache.derby.impl.jdbc.ConnectionChild.newSQLException(Unknown Source)

1 个答案:

答案 0 :(得分:0)

全局使用ResultSet。 getGenreName方法中使用的ResultSet覆盖主searchCharacter方法中的resultSet。解决方案:在每个方法中单独设置ResultSet变量。