Forums Register Login

SQL Select Count

+Pie Number of slices to send: Send
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);

}
+Pie Number of slices to send: Send
"Anonymous Anonymous"

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

-Scott
+Pie Number of slices to send: Send
Select -> use java to compare the result to 3 -> insert
What's brown and sticky? ... a stick. Or a tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1494 times.
Similar Threads
Radiobuttons & comboxes in an SQL query
connectivity problem
how to increment suufix of this string?
Definging a Unique Constraint
Displaying images dynamically from database
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:05:21.