Using HTML and JSP How do i capture checkbox options and Store selected choice into Mysql database. In the example below myChoice would have multiple answer and I want store in db table under one column of one row. (i know i could concatenate the choices) and store in one column but i am finding it hard to accomplish. Precisely i am struggling with capturing checkbox selection from HTML, concatenating choices into one string and storing in table. Any suggestions pls.
<%@page import="java.sql.*" %>
<%@page import="java.util.Date" %>
<% Class.forName("com.mysql.jdbc.Driver"); %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title> My Mood Form</title>
</head>
<body>
<%!
public class Actor {
String URL = "jdbc:mysql://localhost/test";
String USERNAME = "XXXXX";
String PASSWORD = "XXXXX";
Connection connection = null;
PreparedStatement insertActors = null;
ResultSet resultSet = null;
public Actor () {
try {
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
insertActors = connection.prepareStatement(
"INSERT INTO mood(first_name, last_name, my_choice, last_update)"
+ " VALUES (?,?,?,?)");
} catch (SQLException e){
e.printStackTrace();
}
}
public int setActors (String first, String last, String choice, Timestamp timeStamp){
int result =0;
try {
insertActors.setString(1, first);
insertActors.setString(2, last);
insertActors.setString(3, choice);
insertActors.setTimestamp(4, timeStamp);
result = insertActors.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return result;
}
}
%>
<%
int result = 0;
if (request.getParameter("submit") != null){
String firstName = new String ();
String lastName = new String ();
String myChoice = new String ();
//String [] myChoice;
if (request.getParameter("first") != null){
firstName = request.getParameter("first");
}
if (request.getParameter("last") != null){
lastName = request.getParameter("last");
}
if (request.getParameter("choice") != null){
myChoice = request.getParameter("choice");
/**
* for (int i=0; i<selectedInterest.length; i++)
{
out.println(" " + selectedInterest[i] + "");
s = s + selectedInterest[i] + ", ";
out.println("s = " + s );
}
}
else{
out.println("none");
*/
}
Date date = new Date();
Timestamp timeStamp = new Timestamp (date.getTime());
Actor mood = new Actor();
result = mood.setActors(firstName, lastName, myChoice, timeStamp);
}
%>
<h1>Registration Form</h1>
<form name="Registration" action="index.jsp" method="POST">
<table border="0" cellpadding="3">
<tbody>
<tr>
<td>Surname:</td>
<td><input type="text" name="first" size="40" /></td>
</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="last" size="40" /></td>
</tr>
<tr>
<td>My Choice:</td>
<td>
<input type="checkbox" name="choice" value="excited" />I am excited<br>
<input type="checkbox" name="choice" value="happy" />I am happy <br>
<input type="checkbox" name="choice" value="indifference" />I am indifference<br>
<input type="checkbox" name="choice" value="sad" />I am sad<br>
</td>
</tr>
<tr>
<td><button type="reset" value="Reset">Reset</button></td>
<td><button type="submit" value="submit" name="submit">Submit</button></td> </td>
</tr>
</tbody>
</table>
</form>
</body>
Aucun commentaire:
Enregistrer un commentaire