Servlet Oracle的Exususted ResultSet问题

时间:2016-04-08 10:33:28

标签: java sql oracle jdbc

每次运行此代码时,都会给出一个穷举的结果集错误。我不知道我做错了什么,但我尝试删除.next();一个或所有结果集的代码,然后给出的错误是下一个没有调用的ResultSet。

我不确定我做错了什么。只是好奇人们可能认为问题可能是什么?我之前在我的servlet代码中做过类似但只使用了1个语句然后使用了1个预处理语句。这次我使用2个语句和1个预备语句。

String opt1 = req.getParameter("RecName"); //Retrieves info from HTML form
String ingr1 = req.getParameter("Ing1"); //Retrieves info from HTML form

stmt = con.createStatement();
stmt1 = con.createStatement();

ResultSet rs11 = stmt.executeQuery("SELECT recipe_ID FROM GM_Recipes WHERE rec_name='" + op1 + "'"); //choose recipe_ID from sql table
rs11.next();

ResultSet rs12 = stmt.executeQuery("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name='" + ingr1 + "'"); //choose ingredient_ID from sql table
rs12.next();

int olo = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
int olo1 = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int

PreparedStatement pstmt1 = con.prepareStatement("INSERT INTO GM_RecLnk(recipe_ID,ingredient_ID) VALUES (?,?)");
pstmt1.clearParameters();

pstmt1.setInt(1,olo);
pstmt1.setInt(2,olo1);

ResultSet rs1 = pstmt1.executeQuery();
rs1.next();

1 个答案:

答案 0 :(得分:1)

关于代码的一些想法(在评论中)

stmt = con.createStatement();
stmt1 = con.createStatement();

ResultSet rs11 = stmt.executeQuery("SELECT recipe_ID FROM GM_Recipes WHERE rec_name='" + op1 + "'"); //choose recipe_ID from sql table

//Check if you HAVE a line here!
if(!rs11.next()) {
  System.out.println("No Recipe Found");
}

//Use stmt1 - that's why you created it?!    
ResultSet rs12 = stmt1.executeQuery("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name='" + ingr1 + "'"); //choose ingredient_ID from sql table
if(!rs12.next()) {
  System.out.println("No Ingredient Found");
}

int olo = ((Number) rs11.getObject(1).intValue(); //convert resultset value to int
//Read Ingredient from rs12 -> that's where you selected it into
int olo1 = ((Number) rs12.getObject(1).intValue(); //convert resultset value to int

虽然这可能会指出您正确的解决当前问题的方向,但您应该考虑学习干净的代码。

考虑这个代码使用try-with-resource,重构一些方法,使用预处理语句。

   //Replace exiting code
    String opt1 = req.getParameter("RecName"); //Retrieves info from HTML form
    String ingr1 = req.getParameter("Ing1"); //Retrieves info from HTML form

    int recipieId = getRecipeId(con, opt1);
    int ingredientId = getIngredientId(con, ingr1);

    if(recipeId > 0 && ingredientId > 0) {
        //Process result
        insertRecLnk(con, recipeId, ingredientId);
    } else {
        System.out.println("No INSERT");            
    }


//Helper functions

protected int getRecipeId(Connection con, String rec) {
    try(PreparedStatement st = con.prepareStatement("SELECT recipe_ID FROM GM_Recipes WHERE rec_name=?")) {
        st.setString(1, rec);
        try(ResultSet rs11 = st.executeQuery()) {
          //choose recipe_ID from sql table                     
            if(rs11.next()) {
              return rs11.getInt(1);  
            }
        }
    } catch(SQLException e) {
        e.printStackTrace();
    }
    System.out.println("No Recipe Found");
    return -1;
}

protected int getIngredientId(Connection con, String ing) {
    try(PreparedStatement st = con.prepareStatement("SELECT ingredient_ID FROM GM_IngredientDB WHERE ing_name=?")) {
        st.setString(1, ing);
        try(ResultSet rs11 = st.executeQuery()) {
          //choose recipe_ID from sql table                     
            if(rs11.next()) {
              return rs11.getInt(1);  
            }
        }
    } catch(SQLException e) {
        e.printStackTrace();
    }
    System.out.println("No Ingredient Found");
    return -1;
}

protected void insertRecLnk(Connection con, int rId, int iId) {
    try(PreparedStatement ps = con.prepareStatement("INSERT INTO GM_RecLnk(recipe_ID,ingredient_ID) VALUES (?,?)")) {
        ps.setInt(1, rId);
        ps.setInt(2, iId);
        ps.executeUpdate();
    } catch(SQLException e) {
        e.printStackTrace();
    }
}