How to insert multiple checkboxes into Msaccess database J2EE using prepared statement

How to insert multiple checkboxes into Msaccess database J2EE using prepared statement

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.
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 instead of statement.  Create a database: First create a database named... Insert data in mysql database through jsp using prepared statement
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... defined in prepared Statement class, When you want to substitute a question mark
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
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 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 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  ... using Prepared Statement ... Record inserted successfully
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. (adsbygoogle
JDBC Insert Prepared Statement
teaches how to execute the sql statements using the PreparedStatment in Java. Here is the video tutorial of using the prepared statement in Java. Steps to use... to the database"); // Create a query String String query = "INSERT INTO STUDENT
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: 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 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
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
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
Inserting Records using the Prepared Statement
to learn how we will insert the records in the database table by using... InsertRecords Insert records example using prepared statement... Inserting Records using the Prepared Statement  
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
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
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. (adsbygoogle = window.adsbygoogle
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>
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
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
callable statement,stored procedure for insert data and also for update data into oracle database using jsp
callable statement,stored procedure for insert data and also for update data into oracle database using jsp  iam a fresher newbie to softparsing i have one task to do soft parsing tl asked to do callable statement,stored
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 ... are deleting record of student whose roll_no is 3  using prepared statement
how to insert data from database using oops concepts - Development process
how to insert data from database using oops concepts  Hi, How to insert data from database using oops based concepts.please write the code... the following code: import java.sql.*; class Insert{ public static String url
Hibernate Prepared Statement
This section contain how prepared statement works in hibernate
How to insert image in sql database from user using servlet
How to insert image in sql database from user using servlet  pls tell me accept image from user and insert image in sql server 2005 database using servlet and jsp
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
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 dynamic textbox values into database using Java?
How to insert dynamic textbox values into database using Java?  Hi I am trying to insert dynamic textbox values to database, my jsp form have 2... of dynamic textboxes with Name and Address will display....Now I want to Insert
Facing Problem to insert Multiple Array values in database - JSP-Servlet
facing the problem while inserting the data in database. iam using the MsAccess Database My Database structure is Like iam using tow tabel ,CustomerDetails...Facing Problem to insert Multiple Array values in database  Hai
checking index in prepared statement
links:ADS_TO_REPLACE_1 http://www.roseindia.net/jdbc/prepared-statement-insert.shtml http://www.roseindia.net/jdbc/insert-prepared-statement.shtml ThanksADS...checking index in prepared statement  If we write as follows: String
insert multiple selection - Java
insert multiple selection - Java  how to insert multiple selection values from html into database using servlets
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
How to insert data from textfields into database using hibernate?
How to insert data from textfields into database using hibernate?   try{ Session session = HibernateUtil.getSessionFactory().openSession...); System.out.println("Successfully data insert in database"); tx.commit
insert date into database using jsf
insert date into database using jsf  Hello I need a simple example of source code to insert a date into a database using jsf what do I have to put in backing bean and page jsf ( i need juste a date ) thanks
Insert Date using Statement - Development process
Insert Date using Statement   Hi, plz send me code for Storing system date using statement instead of preparedstatement. Thanks Prakash  Hi Friend, Try the following code: Thanks
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
insert data in the database using checkbox
insert data in the database using checkbox  i am fetching data from the database using servlet on the jsp page and there is checkbox corresponding... should i insert only checked data into database on submission.   We
Set Timestamp by using the Prepared Statement
Set Timestamp by using the Prepared Statement... will teach how to set the Timestamp in database table by using... in the database table by using the  java.util.Date package. See brief
How to insert and retreive an image in mysql database using spring mvc framework???
How to insert and retreive an image in mysql database using spring mvc framework???  I ve solved this problem based on the Product Search Application. Those who need this concept and code segment kindly copy it. Thanks and All
to write an SQL query using insert statement
to write an SQL query using insert statement  I need to write an sql query for the below using InSERT command.pls help. Insert 5-star ratings by James Cameron for all movies in the database. Leave the review date as NULL
prepared statement in sqlite
prepared statement in sqlite  How to writer "prepared statement in sqlite" ?   $register->bind_param('ssssssis', $name, $username, $password, $email, $security_answer, $date, $user_level, $security_question); S
how to display selected checkboxes dynamically using jsp
how to display selected checkboxes dynamically using jsp  Hi friends i have a requirement that : in my JSP page i have radio buttons followed by the DB fields..when i click SUBMIT button,if the field(or record
Oracle Database - Insert System Time
Oracle Database - Insert System Time  Hi I want to insert only system time dynamically using a statement but not a prepared statement into Oracle Database please suggest me any code
Count Records using the Prepared Statement
Count Records using the Prepared Statement  ... records example using prepared statement! Enter table name: movies... to count all records of the database table by using the PreparedStatement
How to insert or delete records in MS access database using jsp - JSP-Servlet
How to insert or delete records in MS access database using jsp  Hi friends please provide me a solution that i insert or delete record from a database using java server pages. I used the microsoft access 2003 database. PlZ
How to store multiple images in folder and images path in database at a time using jsp?
How to store multiple images in folder and images path in database at a time using jsp?  I wanna browse multiple images in one form and store them in one folder and their path into datrabase

Ads