Wednesday 16 January 2013

JSTL ans Servlet in Tomcat 7


JSTL in  Servlet Tomcat 7

   Tomcat 7 does not come with JSTL jar files by default.
   First, locate the jar files for both JSTL API and JSTL Implementation. They are located on the JSP Standard Tag Library download page. Click into each repository and find the .jar file.
   
Tomcat 7
           =>Servlet -api-3.0
           =>JSTL-1.2

  Servlet
             Download Servlet -api-3.0
   JSTL
        JSTL API jar - javax.servlet.jsp.jstl-api-1.2.1.jar. 
        JSTL Implementation jar -javax.servlet.jsp.jstl-1.2.1.jar. 
       Download both file in JSP standard Tag Library
               OR
         Download JSTL-1.2.jar
 After download jar file past that file into /WEB-INF/lib
Now create a simple JSP page that uses one of the JSTL Tag Libraries.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title>Show Account List</title>
</head>
<body>
<c:if test="${not empty allAccountList}">
<table style="width="100%" border="1" cellpadding="2" cellspacing="2" style="background-color: #ffffff;" >
<th>Account Number</th>
<th>Customer Name</th>
<th>Current Balance</th>
<th>Maximum Balance</th>
<th>Account Type</th>
<th>Account Pin</th>
<c:forEach var="accountList" items="${allAccountList}">
<tr>
<td>${accountList.accountNumber_}</td>
<td>${accountList.customerName_}</td>
<td>${accountListt.accountBalance_}</td>
<td>${accountList.accountMaximumBalance_}</td>
<td>${accountList.accountType_}</td>
<td>${accountList.accountPin}</td>
</tr>
</c:forEach>
</table>
</c:if>
</body>
</html>

web.xml   
Note: Not compulsory for that modification in web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0"> 

  <!-- Define servlets that are included in-->


 <!-- servlet mappings s>



</web-app>

Friday 11 January 2013

Insert data in database in batches using Java JDBC


  executeBatch- JDBC

In this note, we will see how we can use JDBC APIs like Statement andPreparedStatement to insert data in any database in batches.
              Also we will try to explore scenarios where we can run out of memory and how to optimize the batch operation.
Firs how Insert data in database in batches using Java JDBC.

Bad Code

String [] queries = {
    "insert into employee (name, city, phone) values ('A', 'X', '123')",
    "insert into employee (name, city, phone) values ('B', 'Y', '234')",
    "insert into employee (name, city, phone) values ('C', 'Z', '345')",
};
             
Connection connection = new getConnection();
Statement statemenet = connection.createStatement();
             
for (String query : queries) {
    statemenet.execute(query);
}
statemenet.close();
connection.close();

This is the BAD code. You are executing each query separately. This hits the database for each insert statement. Consider if you want to insert 1000 records. This is not a good idea. We’ll below is the basic code to perform batch insert. Check it out:

Good Code

Connection connection = new getConnection();
Statement statemenet = connection.createStatement();
 
for (String query : queries) {
    statemenet.addBatch(query);
}
statemenet.executeBatch();

statemenet.close();
connection.close();

        we used addBatch() method of Statement, instead of directly executing the query. And after adding all the queries we executed them in one go using statement.executeBatch() method.
             Note that we have taken the queries from a String array. Instead you may want to make it dynamically. For example:

import java.sql.Connection; Nothing fancy, just a simple batch insert.
import java.sql.Statement;
//...
Connection connection = new getConnection();
Statement statemenet = connection.createStatement();
for (Employee employee: employees) {
    String query = "insert into employee (name, city) values('"
            + employee.getName() + "','" + employee.getCity + "')";
    statemenet.addBatch(query);
}
statemenet.executeBatch();
statemenet.close();
connection.close();