For this what I do is as soon as some user fills up the page and clicks 'Save' I invoke a Servlet which checks the information filled in that page with that of the existing records and if a match is found then a message is printed back saying 'record already exists,cannot save twice!'.
I am using PreparedStatement to execute a MySql query in my Servlet and an if-else block to distinguish between an existing record and a fresh record,roughly my code is something like this:-
Expand|Select|Wrap|Line Numbers
- String s = request.getParameter("StudentID");
- String s1 = request.getParameter("SubjectID");
- PreparedStatement pst = con.prepareStatement(select * from abc where (studid=? && subid=?);); //Note: I dont want my table abc to have a duplicate studid-subid combination, one student can study only one subject at a time.
- pst.setInt(1,Integer.parseInt(s));
- pst.setInt(2,Integer.parseInt(s1));
- ResultSet rs = null;
- rs = pst.executeQuery();
- if(rs != null)
- {
- //means the record already exists in table 'abc' so dont save this record twice,instead alert the user'This Record already exists!'
- }
- else
- {
- //since ResultSet is null that means no matching record was found in table'abc' indicating that the user has entered a fresh record so carry on the processing...
- }
My question is if some query doesnt yield a result then shouldnt the Resultset associated with it be null or perhaps empty?Is there a way of checking that?
..or should I rather check in a while-loop[while(rs.next())] instead of saying if(rs!=null)?
Please guide ..Thanks in advance!