How to store radio button value in Database?
I have a JSP page, on w开发者_如何学Pythonhich I have radio button giving a choice of male or female.
When I click on a radio button then that particular entry is not get submitted in my database.
How should I store that value in database?
Your question is overly broad. There are so many things which needs to happen between "click on a radio button" and "submitted in database". You are not specific about which step exactly fails. It would be nice if you put in some effort by yourself to naildown the exact step which fails. This way we can faster provide a well-suited answer. You can use Firebug or Fiddler2 to track HTTP requests/responses. You can use the IDE-builtin debugger to track the code execution of Java code step by step and all variables currently in scope/memory. You can use a logger in Java side and/or a SQL monitor in DB side to track the SQL queries.
Anyway, here's a kickoff example of how things are supposed to happen in its most simplest form so that you can compare it with your setup and naildown the issue.
Have a nullable
gender
column in yourUser
table which accepts the set of valuesM
orF
(which stands for male or female).Have a form in your JSP which allows the user to select it.
<form action="servleturl" method="post"> ... <br /> <input type="radio" name="gender" value="M" /> Male <br /> <input type="radio" name="gender" value="F" /> Female <br /> <input type="submit" /> </form>
Have a servlet which listens on
/servleturl
and gathers the submitted value indoPost()
.// ... String gender = request.getParameter("gender"); // ...
Have a DAO class which does the following:
// ... preparedStatement = connection.prepareStatement("INSERT INTO User (col1, col2, col3, gender) VALUES (?, ?, ?, ?)"); // ... preparedStatement.setString(4, gender); // ... preparedStatement.executeUpdate(); // ...
精彩评论