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...
Ads
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... the code.set String ( ) - This is a method
defined in
prepared Statement class
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 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
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
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 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
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
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
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
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
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
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