• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

SQL Select Count

 
Tim Jones
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following is sql code which checks for multiple �titles� with the same value (less than or equal to 3)

"SELECT count(*)<=3 FROM module WHERE title='?'".

I am trying to write a snippet of code that does a select count(*) from module where title=? (above code), and then if the result of that is 3=3 then do the INSERT (following code) else display an error message without inserting the record.

In simple terms the following code should only execute if the above code is true, else display a message without saving. can someone please help me out as I am kind of stuck.

PreparedStatement new_module = connection.prepareStatement("INSERT INTO module (room_number, module_code, title, provider, duration, time_slot, weekdays)"+ "VALUES(?,?,?,?,?,?,?)");

new_module.setString(1, request.getParameter("room_number"));
new_module.setString(2, request.getParameter("module_code"));
new_module.setString(3, request.getParameter("title"));
new_module.setString(4, request.getParameter("provider"));
new_module.setInt(5, 1);
new_module.setString(6, request.getParameter("time_slot"));
new_module.setString(7, request.getParameter("weekdays"));

new_module.executeUpdate();
connection.commit();
new_module.close();
response.sendRedirect("index.jsp");
connection.setAutoCommit(savedCommitValue);

}
 
Scott Selikoff
author
Saloon Keeper
Posts: 4033
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"Anonymous Anonymous"

Welcome to the JavaRanch. Please review the JavaRanch Naming Policy and update your name accordingly.

-Scott
 
Marilyn de Queiroz
Sheriff
Posts: 9068
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Select -> use java to compare the result to 3 -> insert
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic