简体   繁体   中英

Getting next value from sequence

I have a bit of code here to get the next value of my sequence, but it is adding the total number of records onto the result each time.

I'm only learning about prepared Statements, I'm thinking this is something small, maybe rset.next() should be something else?

public void add( String title, String actor, String genre ) {
    try {
        String sql2 = "Select movie_seq.nextval from Movie";
        pstmt = conn.prepareStatement(sql2);

            rset = pstmt.executeQuery();
            int nextVal = 0;
            if(rset.next())
                nextVal = rset.getInt(1);



        String queryString = "Select MovieID, Title, Actor, Genre from Movie";
        pstmt = conn
                .prepareStatement(queryString,
                        ResultSet.TYPE_SCROLL_SENSITIVE,
                        ResultSet.CONCUR_UPDATABLE);
        rset = pstmt.executeQuery();

        rset.moveToInsertRow();
        rset.updateInt(1, nextVal);
        rset.updateString(2, title);
        rset.updateString(3, actor);
        rset.updateString(4, genre);
        rset.insertRow();
        pstmt.executeUpdate();

    } catch (SQLException e2) {
        System.out.println("Error going to previous row");
        System.exit(1);
    }
}

Any help appreciated.

I think you don't need the call to pstmt.executeUpdate();

As stated in ResultSet doc , the function insertRow stores the row in the Dataset AND in the database.

The following code shows all that's necessary to add a new row:

   rset.moveToInsertRow(); // moves cursor to the insert row
   rset.updateString(1, "AINSWORTH"); // updates the
      // first column of the insert row to be AINSWORTH
   rset.updateInt(2,35); // updates the second column to be 35
   rset.updateBoolean(3, true); // updates the third column to true
   rset.insertRow();
   rset.moveToCurrentRow();

Why dont you iterate using while rather than if . something like this

List lst = new ArrayList();

Someclass sc = new SomeClass(); //object of the class 

    String query = "SELECT * from SomeTable";
                PreparedStatement pstmt = sqlConn.prepareStatement(query);
                ResultSet rs = pstmt.executeQuery();
                Role role = null;
                while (rs.next()) {
                    String one = rs.getString(1);
                    String two = rs.getString(2);
                    boolean  three = rs.getBoolean(3);
                    //if you have setters getters for them 
                               sc.setOne(one);
                               sc.setTwo(two);
                               sc,setThree(three);
                           lst.add(sc)
                                    }
//in the end return lst which is of type List<SomeClass>
            }

Shouldn't you be doing this instead?:

String sql2 = "Select " + movie_seq.nextval + " from Movie";

As it is, it seems like you're passing a slightly bogus string into the SQL query, which is probably defaulting to the max index (not 100% positive on that). Then rs.next() is just incrementing that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM