0

I'm using the following code for getting values from ArrayList and adding into database. I'm using PreparedStatement and my database is Oracle.

ps = conn.prepareStatement("insert into Employee values(?,?,?,?,?)");
for (Employee e : elist) {
    ps.setInt(1, e.getEmployeeId());
    ps.setString(2, e.getEmployeeName());
    ps.setString(3, e.getGender());
    ps.setString(4, e.getDesignation());
    ps.setString(5, e.getEmail());
    added += ps.executeUpdate();
}       

elist is an ArrayList of Employees.

here is the code for preparing the ArrayList:

Employee e1 = new Employee(1,"I","M","AS","[email protected]");
Employee e2 = new Employee(2,"A","M","RS","[email protected]");
Employee e3 = new Employee(3,"S","F","SD","[email protected]");
Employee e4 = new Employee(4,"SS","F","SD","[email protected]");
ArrayList<Employee> elist = new ArrayList<Employee>();
elist.add(e1);
elist.add(e2);
elist.add(e3);
elist.add(e4);   

I've uploaded of the Employee table after insertion:

enter image description here

As you can see, the rows are not inserted as per the sequence of primary key i.e. Employee_Id values.I mean to say, when the data is inserted, the rows are normally expected to be in the sequence 1,2,3,4 . But it's not the case here.

I might have missed some very basic point in this, but I I've just begin with JDBC stuff.

10
  • What's the error? Btw, you are probably missing the column names in your INSERT statement. Commented Nov 21, 2015 at 18:47
  • Nothing. No error is there . I've attached the snapshot of resulting table. And regarding the column names, it also works without that. Commented Nov 21, 2015 at 18:52
  • 2
    So what's the problem? Commented Nov 21, 2015 at 18:52
  • 1
    If there is no error then why you asked question? Commented Nov 21, 2015 at 18:55
  • 1
    Rows in a relational database are not sorted. If you don't use an order by when retrieving the data the database is free to return them in any order it likes. Commented Nov 21, 2015 at 19:00

1 Answer 1

3

SQL tables are unsorted sets. They have no intrinsic order. If you want to retrieve the values sorted by the employee id, you need to explicitly state it in your order by clause:

SELECT * FROM employee ORDER BY employee_id ASC
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.