Home Jsp Java Multiple Insert Query
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Multiple Insert Query

Advertisement
This example explains you about how to execute batch insert in Java. This example explains all the steps for executing the multiple insert query. In this example we will use the Eclipse IDE for compiling and the Tomcat 7 server for deploying the web application. In this example we will get the data into the dropdown list dynamically and then we will select the multiple list from this dropdown list and then we will store all the selected values into the database table.

Java Multiple Insert Query

In this example we will discuss about how to execute multiple insert query in Java.

This example explains you about how to execute batch insert in Java. This example explains all the steps for executing the multiple insert query. In this example we will use the Eclipse IDE for compiling and the Tomcat 7 server for deploying the web application. In this example we will get the data into the dropdown list dynamically and then we will select the multiple list from this dropdown list and then we will store all the selected values into the database table.

Example

Here I am giving a simple example that demonstrates how to execute the multiple sql insert query in JSP. In this example we will first create the two database table. From one of which we will get the value and displayed them into the dropdown list dynamically and in the other table we will store the multiple selected value of dropdown list. Then I have created the two JSP pages one for getting the value into the dropdown list dynamically and the other is for getting the multiple selected value from dropdown list and then to store them into the table. To get the multiple selected items from the dropdown list into the JSP page I have use the request.getParameterValues(String selectTagName); The getParameterValues() method returns the array of String.

Database tables

CREATE TABLE `fruitorder` (             
              `fruitName` varchar(15) DEFAULT NULL  
            ) ENGINE=InnoDB DEFAULT CHARSET=latin1 

And Inserted some values like as follows :

insert into `fruitorder`(`fruitName`) values ( 'Apple')
insert into `fruitorder`(`fruitName`) values ( 'Orange')
insert into `fruitorder`(`fruitName`) values ( 'Grapes')
insert into `fruitorder`(`fruitName`) values ( 'Guava')
insert into `fruitorder`(`fruitName`) values ( 'Pineapple')

And the other table is as follows :

CREATE TABLE `fruitpurchase` (          
                 `fruitName` varchar(15) DEFAULT NULL  
               ) ENGINE=InnoDB DEFAULT CHARSET=latin1  

fruitpurchase table is empty now, in this table we will store the value after selecting the multiple values from the dropdown list and submission of form.

JSP Code

getValueIntoDropdownList.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Drop Downlist</title>
</head>
<body>
<%! String driverName = "com.mysql.jdbc.Driver";%>
<%!String url = "jdbc:mysql://localhost:3306/record";%>
<%!String user = "root";%>
<%!String psw = "root";%>
<form action="storeValueOfDropdownList.jsp">
<%
Connection con = null;
PreparedStatement ps = null;
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,user,psw);
String sql = "SELECT * FROM fruitOrder";
ps = con.prepareStatement(sql);
ResultSet rs = ps.executeQuery(); 
%>
<p>Select Fruits For Purchase : </p>
<p>
<select name="select" multiple="multiple" tabindex="1">
<%
while(rs.next())
{
String fname = rs.getString("fruitName"); 
%>
<option value="<%=fname %>"><%=fname %></option>
<%
}
%>
</select>
</p>
<p><input type="submit" value="purchase"/>
<%
}
catch(SQLException sqe)
{ 
out.println(sqe);
}
%>
</form>
</body>
</html>

storeValueOfDropdownList.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Drop Downlist</title>
</head>
<body>
<%! String driverName = "com.mysql.jdbc.Driver";%>
<%!String url = "jdbc:mysql://localhost:3306/record?allowMultiQueries=true";%>
<%!String user = "root";%>
<%!String psw = "root";%>
<form action="#">
<%
Connection con = null;
PreparedStatement ps = null;
String[] fruitValues = request.getParameterValues("select");
try
{
Class.forName(driverName);
con = DriverManager.getConnection(url,user,psw);
String sql = "INSERT INTO fruitPurchase(fruitName) Values(?)";
String value = "";
ps = con.prepareStatement(sql);
for(int i = 0; i<fruitValues.length; i++)
{ 
value = fruitValues[i];
ps.setString(1, value); 
ps.addBatch(); 
}
ps.executeBatch();
out.println("Value Added Successfully");
ps.close();
con.close();
}
catch(SQLException sqe)
{ 
out.println(sqe);
}
%>
</form>
</body>
</html>

Output

The fruitorder table is as follows :

When you will execute the getValueIntoDropdownList.jsp page then you will get the first like as follows :

When you will select the multiple values into the dropdown list and then click on the button named "purchase" then the value will be stored into the fruitpurchase table and a message of successful submission will be displayed on the page as follows :

And the fruitpurchase table will be as follows :

Download the Source Code

Download the Source Code as WAR file and rename it to jspSotreMultipleSelectedValue.war and then import this file into the Eclipse and then run this example

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 22, 2013

Related Tutorials

Ask Questions?    Discuss: Java Multiple Insert Query  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
DMCA.com