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:
Advertisements
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
JDBC: Insert Record using Prepared StatementsJDBC:
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
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
JDBC: Select Records using Prepared StatementJDBC: 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+jsphow 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 & retrivehow 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 jsphow 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 jsphow 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 databasecreate 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
create bar chart in jsp using msaccess databasecreate 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 MYSQLPrepared 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
JDBC: Delete Record using Prepared StatementJDBC: 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 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 checkboxesUpdating
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
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
difference between prepared statement and statementdifference 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
insert date into database using jsfinsert 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
prepared statementprepared 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 checkboxinsert 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
to write an SQL query using insert statementto 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 sqliteprepared 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
Oracle Database - Insert System TimeOracle
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