2

I am inserting a row using

String sql="insert into OCS_TBL_DOCTOR values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement ps=con.prepareStatement(sql);
        ps.setString(1, doctor.getDoctorID());
        ps.setString(2, doctor.getDoctorName());
        ps.setString(3, doctor.getDateOfBirth());
        ps.setString(4, doctor.getDateOfJoining());
        ps.setString(5, doctor.getGender());
        ps.setString(6, doctor.getQualification());
        ps.setString(7, doctor.getSpecialization());
        ps.setInt(8, doctor.getYearsOfExperience());
        ps.setString(9, doctor.getStreet());
        ps.setString(10, doctor.getLocation());
        ps.setString(11, doctor.getCity());
        ps.setString(12, doctor.getState());
        ps.setString(13, doctor.getPincode());
        ps.setString(14, doctor.getContactNo());
        ps.setString(15, doctor.getEmaiID());
        ResultSet rs = ps.executeQuery();

I want to know how will I get that last entry that I have just inserted. Here DoctorID is the primary key.

  • Make a select on `id` equal to whatever you pass as `doctor.getDoctorId`?.. – Sergey Kalinichenko Apr 04 '15 at 10:18
  • select the record back out again? – pala_ Apr 04 '15 at 10:18
  • Since you're setting the PK explicitly, then you *already know* its value! If, on the other hand (and contrary to the code you have shown), you are *not* setting an explicit value for `DoctorID` and it happens to be an integer column with the `AUTO_INCREMENT` attribute, you can obtain the "last insert id" from Java—see http://stackoverflow.com/a/1915197/623041. – eggyal Apr 04 '15 at 10:19
  • I am using the AUTO_INCREMENT for DoctorID. what I am sending through the query is null value so that whenever the database executes the query, the null value is encountered, and the DoctorID is configured automatically. – Swagato Dey Apr 05 '15 at 19:07

2 Answers2

0
String sql="select * fom OCS_TBL_DOCTOR WHERE doctor_id_in_database="+doctor.getDoctorID();
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
0

There are two options:

Firstly as it is an insert statement and you are specifying the DoctorID to be persisted, you can always requery on that field or you can create an index for DoctorID and get the maximum.

Secondly, you can create another column which stores created/modified date and let the database stamp system time to it. Once the insert is done, you can query based on this column and add order by desc.

Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57
  • Actually I am Using an auto increment for DoctorID. The Value that i am sending through the query is null. Therefore i wont be knowing that DoctorID – Swagato Dey Apr 05 '15 at 19:04
  • Can you please elaborate about the time stamp? I guess that might do the trick – Swagato Dey Apr 05 '15 at 19:05
  • @Swagato, If you add a new column, in the database which stores date and while populating each row, use system date to be stored in that column. this would assist us in ordering this column in descending order so as to retrieve the most recent record. – Saurabh Jhunjhunwala Apr 06 '15 at 04:16