简体   繁体   中英

How to find last entry to a Database

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.

String sql="select * fom OCS_TBL_DOCTOR WHERE doctor_id_in_database="+doctor.getDoctorID();

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.

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