Batch insert in Jsp

addBatch.jsp



<%
        Connection conn = null;
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String jdbcURL="jdbc:mysql://localhost:3306/jsp";
        
        conn = DriverManager.getConnection(jdbcURL,"root", "");
        
        PreparedStatement psInsertRecord=null;
        String sqlInsertRecord=null;
        int[] iNoRows=null;
%>

<%
sqlInsertRecord="insert into class_room (sStudentName, sRollNo, sCourse) values(?,?,?)";
    psInsertRecord=conn.prepareStatement(sqlInsertRecord);
    
    for(int i=1;i<=50;i++)
    {
      psInsertRecord.setString(1,"New Student");
      psInsertRecord.setString(2,"4"+i);
      psInsertRecord.setString(3,"10th Class");
      psInsertRecord.addBatch(); 
    }
    iNoRows=psInsertRecord.executeBatch();
%>
Row inserted into table is  <%=iNoRows.length%> 

<%
       try{
          if(psInsertRecord!=null)
          {
            psInsertRecord.close();
          }
          
          if(conn!=null)
          {
           conn.close();
          }
        }
        catch(Exception e)
        {
          e.printStackTrace(); 
        }
%>


addBatchStatement.jsp


<%@ page language="java" import="java.sql.*" errorPage="" %>
<%
        Connection conn = null;
        
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        String jdbcURL="jdbc:mysql://localhost:3306/jsp";
    
        conn = DriverManager.getConnection(jdbcURL,"root", "");
        
        Statement stInsertRecord=null;
        String sqlInsertRecord=null;
        int[] iNoRows=null;
%>


Add Batch with Statement in JDBC



<%
stInsertRecord=conn.createStatement();

for(int i=1;i<=50;i++)
{
sqlInsertRecord="insert into class_room (sStudentName, sRollNo, sCourse)" +
                                      " values('New Student','4"+i+"','10th Class')";
stInsertRecord.addBatch(sqlInsertRecord); 
}

iNoRows=stInsertRecord.executeBatch();
%>
Row inserted into table is  <%=iNoRows.length%> 


<%
       try{
          if(stInsertRecord!=null)
          {
            stInsertRecord.close();
          }
          
          if(conn!=null)
          {
           conn.close();
          }
        }
        catch(Exception e)
        {
          e.printStackTrace(); 
        }
%>