Karen
How to insert multiple checkboxes into Msaccess database J2EE using prepared statement
3 Answer(s)      5 years and a month ago
Posted in : Java Beginners
Dear Experts,

Tried as I might looking for solutions to resolve my Servlet problem, I still can't resolve it. Hope that you can share with me the solutions.

In my html form which has a post, I have the following code:-

<td><h3><b>Subjects to be tutored :</b></h3></td>
<table width="800" border="0" cellspacing="0" cellpadding="0" style="border:1px solid #CFE5FF; background:#fff;">
<tr> <th>Lower Primary</th>
<td><input type="checkbox" name="txtsubjects[]" value="English">English
<input type="checkbox" name="txtsubjects[]" value="Chinese">Chinese
<input type="checkbox" name="txtsubjects[]" value="Math">Math
<input type="checkbox" name="txtsubjects[]" value="Science">Science </td>
</tr>
</table>

And in my servlet, I'm stuck because when I'm trying to use the prepared statement to do the checked boxes part, I'm not sure how to go about doing it.

What I did is :-

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection("jdbc:odbc:user");
String strSubjects[] = request.getParameterValues("txtsubjects");
int subjects = strSubjects.length;

strCreateRecordSQL = "INSERT INTO Parents";
strCreateRecordSQL += "(strID, strFullName,strEmail,strAddress,"
+ " strLocation, strContact, strType, strSubjects, "
+ " strFrequency, strTutorspref, stBudget, StrRemarks) VALUES (?,?,?,?,?,?,?,?,?,?,?,?)";
ps = con.prepareStatement(strCreateRecordSQL);
//Fill up the blanks (?) in the SQL with the form data
ps.setString(1, strID);
ps.setString(2, strFullName);
ps.setString(3, strEmail);
ps.setString(4, strAddress);
ps.setString(5, strLocation);
ps.setString(6, strContact);
ps.setString(7, strType);
if (strSubjects != null){
for(int i=0; i <strSubjects.length; i++){
ps.setString(8, strSubjects[i]);
}
}

I'm not sure how to do the part at ps.setString there and also do I have to create different columns for strSubjects because if the user tick all the 4 subjects it will have to go into 4 separate columns right.
Advertisement
View Answers

May 10, 2010 at 4:24 PM


Hi Friend,

There is no need to create different columns for different subjects, you can insert the multiple subjects in one column.

Try the following code:

1)checkbox.jsp:

<html>
<form method="post" action="../Insert">
<table>
<tr><td>Languages</td><td><input type="checkbox" name="lang" value="Hindi">Hindi<input type="checkbox" name="lang" value="English">English<input type="checkbox" name="lang" value="French">French<input type="checkbox" name="lang" value="German">German</td></tr>
<tr><td><input type="Submit" value="Submit"></td></tr>
</form>
</html>

2)Insert.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;

public class Insert extends HttpServlet{
public void doPost(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String checkValues[]=req.getParameterValues("lang");
String value="";
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn =DriverManager.getConnection("jdbc:odbc:access");
for(int i=0;i<checkValues.length;i++){
value+=checkValues[i]+" ";
}
Statement stmt = conn.createStatement();
int j=stmt.executeUpdate("insert into Student(Subjects) values('"+value+"')");
out.println("Inserted successfully");
}
catch(Exception e){}
}
}

Thanks


May 12, 2010 at 4:38 PM


Dearest Guru Deepak,

I want to THANK YOU so much for your help towards my question.

After looking again and again my program, I have corrected a few more errors, mainly in the jsp portion and it's working fine now - meaning, the data goes to my Ms Access Database.

You are the GREATEST !!! You have made Java Programming not as fearful as it is, not as a MYTH.

Thank you. I'll now work on the filtering part....Thank you. You make it possible for me to continue on this project. You don't know how much it meant to me. Bless you.


November 9, 2010 at 2:59 PM


Hi Dear Karen You can also contact with me about Java Servlet Programming

Bintu Chaudhary...










Related Tutorials/Questions & Answers:
How to insert multiple checkboxes into Msaccess database J2EE using prepared statement - Java Beginners
How to insert multiple checkboxes into Msaccess database J2EE using prepared... statement to do the checked boxes part, I'm not sure how to go about doing... for different subjects, you can insert the multiple subjects in one column
Insert data in mysql database through jsp using prepared statement
; This is detailed jsp code that how to insert data into database by using prepared statement... Insert data in mysql database through jsp using prepared statement...; <HEAD> <TITLE>insert data using prepared statement </TITLE>
Advertisements
Insert data in mysql database through jsp using Prepared Statement ---- Please Resolve it
Insert data in mysql database through jsp using Prepared Statement ---- Please Resolve it   I have tried the following code Database creation... link: http://www.roseindia.net/jsp/prepared-statement-query.shtml   
Insert data in mysql database through jsp using Prepared Statement ---- Please Resolve it
Insert data in mysql database through jsp using Prepared Statement ---- Please Resolve it   I have tried the following code Database creation...;Please visit the following link: http://www.roseindia.net/jsp/prepared-statement
JDBC Prepared Statement Insert
JDBC Prepared Statement Insert   ... Statement Insert. In this Tutorial  the code describe the include a class... the code.set String ( )  - This is a method defined in prepared Statement class
how to update combobx's selected value to database with respect toselected multiple checkboxes
how to update combobx's selected value to database with respect toselected multiple checkboxes   suppose this if form1.jsp in which i am going to display records from database out.println("<tr><td>"+"<input type
how to create bar chart in jsp using msaccess database
how to create bar chart in jsp using msaccess database  type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array
how insert multiple images in mysql database with use of struts 1.3.8 or java method, with single bean,or using array  i am using netbeans 7.0 ,with struts 1.3.8 and i want to insert multiple images in mysql database ,with use
How to insert multiple drop down list data in single column in sql database using servlet
How to insert multiple drop down list data in single column in sql database using servlet  i want to insert date of birth of user by using separate drop down list box for year,month and day into dateofbirth column in sql server
JDBC: Insert Record using Prepared Statements
JDBC: Insert Record using Prepared Statements In this section, you will learn how to insert row using Prepared Statements. Insert Record  ...(); } } } Output : Insert Record using Prepared Statement
JDBC: Select Records using Prepared Statement
JDBC: Select Records using Prepared Statement In this section, you will learn how to retrieve records of table using Prepared Statement. Select Records   : Prepared Statement is precompiled SQL Statements which are stored
how to insert multiple columns of a single row into my sql database using jsp
how to insert multiple columns of a single row into my sql database using jsp  hi sir, how to insert multiple columns of a single row into my sql database using jsp. when i click ADD ROW,rows are added.when i click submit
JDBC Insert Prepared Statement
; } JDBC Insert PreparedStatement PreparedStatement represents a precompiled SQL statement. It is alternative to Statement At first Create named student a table in MySQL database as CREATE TABLE `student` ( `rollno` int(11
Update Record using Prepared Statement
JDBC: Update Record using Prepared Statement In this section, you will learn how to update row using Prepared Statements. Update Record   : Prepared statement is good to use where you need to execute same SQL statement
create bar chart in jsp using msaccess database
create bar chart in jsp using msaccess database  type Exception report message description The server encountered an internal error () that prevented it from fulfilling this request. exception
Prepared statement JDBC MYSQL
Prepared statement JDBC MYSQL  How to create a prepared statement in JDBC using MYSQL? Actually, I am looking for an example of prepared statement.   Selecting records using prepared statement in JDBC
Hibernate Prepared Statement
This section contain how prepared statement works in hibernate
create bar chart in jsp using msaccess database
create bar chart in jsp using msaccess database  thanks for reply, that code i can use but i get the below error, pls help me message description The server encountered an internal error () that prevented it from
fetch and insert multiple rows into mysql database using jsp servlet
fetch and insert multiple rows into mysql database using jsp servlet  ... jsp form and want inserting it into my mysql database table. but i am having a problem to insert multiple rows into database using a single insert query
JDBC: Delete Record using Prepared Statement
JDBC: Delete Record using Prepared Statement In this section, we will discuss how to delete row of a table using Prepared Statements. Delete Record ... of student whose roll_no is 3  using prepared statement. package jdbc
PDO Prepared Statement
statements and how to use it using PDO. Sometimes it is more commodious for us to use a Prepared Statement for sending SQL statements to the database..., using this we can reduce the execution time. The prepared statement can
Inserting Records using the Prepared Statement
to learn how we will insert the records in the database table by using... Inserting Records using the Prepared Statement  ... records example using prepared statement! Enter movie name: Bagban
how to insert data into database using jsp & retrive
how to insert data into database using jsp & retrive  Hello, I have created 1 html page which contain username, password & submit button. in my oracle10G database already contain table name admin which has name, password
how to insert data in database using html+jsp
how to insert data in database using html+jsp  anyone know what... and database name. Here machine name id localhost and database name..."; // declare a connection by using Connection interface
Updating multiple value depending on checkboxes
Updating multiple value depending on checkboxes  Hi .. I want to Update the multiple values of database using checkboxes and want to set the session for selected checkboxes..? please answer if any one knows as soon as possible
how to insert checkbox value into database using jsp
how to insert checkbox value into database using jsp  How to insert check box value to the oracle database using jsp? I want to create hotel's package. where the admin will select the activities to insert in the PACKAGE table. I
how to insert checkbox value into database using jsp
how to insert checkbox value into database using jsp  How to insert check box value to the oracle database using jsp? I want to create hotel's..."); Statement st=con.createStatement(); int i=st.executeUpdate("insert
prepared statement
prepared statement  plese give me a code that have preparedstatement interface and uses a select query having condition date="s"; where s is the date, but this date parameter passed as the string
checking index in prepared statement
links: http://www.roseindia.net/jdbc/prepared-statement-insert.shtml http://www.roseindia.net/jdbc/insert-prepared-statement.shtml Thanks...checking index in prepared statement  If we write as follows: String
how to insert checkbox value into database using jsp
how to insert checkbox value into database using jsp  how to insert check box value to the database using jsp my code is <link href="font&colors.css" rel="stylesheet" type="text/css"> <p></p>
difference between prepared statement and statement
difference between prepared statement and statement  i mean in prepared statement we write insert command as INSERT INTO tablename VALUES(?,?) but in normal statement we write insert into tablename(jtextfiled1.gettext