开发者

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.

  1. Have a nullable gender column in your User table which accepts the set of values M or F (which stands for male or female).

  2. 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>
    
  3. Have a servlet which listens on /servleturl and gathers the submitted value in doPost().

    // ...
    String gender = request.getParameter("gender");
    // ...
    
  4. 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();
    // ...
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜