使用ArrayList在JDBC中插入,不按顺序给出行

时间:2015-11-21 18:47:00

标签: java oracle jdbc

我正在使用以下代码从ArrayList获取值并添加到数据库中。我正在使用PreparedStatement,我的数据库是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是员工的ArrayList

这是准备ArrayList

的代码
Employee e1 = new Employee(1,"I","M","AS","abcd@gmail.com");
Employee e2 = new Employee(2,"A","M","RS","efgh@yahoo.in");
Employee e3 = new Employee(3,"S","F","SD","efgh@gmail.com");
Employee e4 = new Employee(4,"SS","F","SD","abcd@yahoo.in");
ArrayList<Employee> elist = new ArrayList<Employee>();
elist.add(e1);
elist.add(e2);
elist.add(e3);
elist.add(e4);   

我在插入后上传了Employee表:

enter image description here

正如您所看到的,行未按主键序列插入,即Employee_Id值。我的意思是说,插入数据时,行通常应该在序列中1,2,3 ,4。但情况并非如此。

我可能已经错过了一些非常基本的观点,但我只是从JDBC开始。

1 个答案:

答案 0 :(得分:3)

SQL表是未排序的集合。他们没有内在的秩序。如果要检索按员工ID排序的值,则需要在order by子句中明确说明它:

SELECT * FROM employee ORDER BY employee_id ASC