Friday, March 30, 2012

Insert google map in application

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps JavaScript API v3 Example: Geocoding Simple</title>
<link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="//maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
  var geocoder;
  var map;
  function initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(-34.397, 150.644);
    var myOptions = {
      zoom: 8,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  }

  function codeAddress() {
    var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        map.setCenter(results[0].geometry.location);
        var marker = new google.maps.Marker({
            map: map,
            position: results[0].geometry.location
        });
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  }
</script>
</head>
<body onload="initialize()">
  <div>
    <input id="address" type="text" value="Sydney, NSW">
    <input type="button" value="Geocode" onclick="codeAddress()">
  </div>
<div id="map_canvas" style="width:400px; min-width:400px; height:400px; min-height:400px; top:30px; border: solid red 3px;">
</div>
</body>
</html>

Google API's

URL to search place using zip code

http://maps.googleapis.com/maps/api/geocode/json?address=17611&sensor=true

Thursday, March 29, 2012

DAO Layer for applications


package com.anshul.projects.drfirst.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;


import com.anshul.projects.drfirst.commons.Constants;

public class LocalDBConnection implements Constants{
                private static Connection conn = null;
                private static Statement stmt = null;
                private static PreparedStatement pstmt = null;
                private static String url = "";
               
                public static Connection getConnection(String dbName) {
                                try {
                                                if (conn != null && !conn.isClosed()) {
                                                                return conn;
                                                }
                                } catch (SQLException e) {
                                                System.out.println("Unable to estalish Connection because of "+e.getLocalizedMessage());
                                }
                                conn = makeAConnection(dbName);
                                return conn;
                }
               
                private static Connection makeAConnection(String dbName){
                                try {
                                                String username = null;
                                                String password = null;
                                                Class.forName(Drivers.MY_SQL);
                                                if (dbName.trim().equalsIgnoreCase(Databases.LOCAL_DB_NAME)) {
                                                                url = URLs.LOCAL_DB_URL;
                                                                username = Users.LOCAL_DB_USERNAME;
                                                                password = Passwords.LOCAL_DB_PASSWORD;                                               
                                                }
                                                conn = DriverManager.getConnection(url, username, password);
                                                System.out.println("Connection successful");
                                                return conn;

                                } catch (Exception e) {
                                                e.printStackTrace();
                                }
                                return null;
                }
               
                public static void closeConnection() {
                                if (conn != null) {
                                                try {
                                                                conn.close();
                                                                System.out.println("Connection closed.");
                                                                conn = null;
                                                } catch (SQLException e) {
                                                                e.printStackTrace();
                                                }
                                }
                                if(stmt !=null){
                                                try {
                                                                stmt.close();
                                                                System.out.println("Connection stmt closed.");
                                                                stmt = null;
                                                } catch (SQLException e) {
                                                                e.printStackTrace();
                                                }
                                }
                }
                public static Statement getStatement(String dbName) {
                                if (stmt != null) {
                                                return stmt;
                                }
                                try {
                                                stmt = LocalDBConnection.getConnection(dbName).createStatement();
                                                return stmt;
                                } catch (SQLException e) {
                                                e.printStackTrace();
                                }
                                return null;

                }
               
                public static PreparedStatement getPreparedStatement(String dbName, String query) {
                                if (pstmt != null) {
                                                return pstmt;
                                }
                                try {
                                                pstmt = (PreparedStatement) LocalDBConnection.getConnection(dbName).prepareStatement(query);
                                                return pstmt;
                                } catch (SQLException e) {
                                                e.printStackTrace();
                                }
                                return null;

                }
               
                public static void main(String args[]){
                                String query = "select * from update_commands";
                                try {
                                                ResultSet st = LocalDBConnection.getStatement(Databases.LOCAL_DB_NAME).executeQuery(query);
                                } catch (SQLException e) {
                                                e.printStackTrace();
                                }
                }
               
}



package com.anshul.projects.drfirst.test;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.anshul.projects.drfirst.commons.Constants.Databases;
import com.anshul.projects.drfirst.dao.LocalDBConnection;

public class TestMe {
      public static void main (String args[]){
            String query = "insert into login (username, password) values (?,?)";
            PreparedStatement pstmt = LocalDBConnection.getPreparedStatement(Databases.LOCAL_DB_NAME, query);
            try {
                  pstmt.setString(1, "A'nshul");
                  pstmt.setString(2, "S\"ood");
                  int j = pstmt.executeUpdate();
                  System.out.println(j);
                  LocalDBConnection.closeConnection();
            } catch (SQLException e) {
                  e.printStackTrace();
            }
      }
}

Tuesday, March 13, 2012

Creating Custom functions in JSP using JSTL

1. Create a tld file inside WEB-INF folder having declaration of the custom function as below
    <?xml version="1.0" encoding="UTF-8" ?>
      <taglib xmlns="http://java.sun.com/xml/ns/j2ee"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
          version="2.0">
   
          <description>JSTL 1.1 functions library</description>
          <display-name>JSTL functions</display-name>
          <tlib-version>1.1</tlib-version>
          <short-name>fn</short-name>
          <uri>http://java.sun.com/jsp/jstl/functions</uri>
        <function>
              <name>functionName</name>
              <function-class>anshul.project.vaidya.test.Anshul</function-class>
              <function-signature>
                java.util.ArrayList&lt;java.util.ArrayList&gt; functionName( java.lang.String )
          </function-signature>
        </function>
    </taglib>

2.     Configure this tld file in web.xml as
    <jsp-config>
          <taglib>
                <taglib-uri>$customURI</taglib-uri>
                <taglib-location>$Absolute path of tld file inside WEB-INF starting with / </taglib-location>
          </taglib>
      </jsp-config>

3.     Now to use the function in a jsp page add the taglib directive there as

    <%@ taglib uri="$customURI" prefix="$customPrefix"%> 
     
    Now you can use the function anywhere in the page like:-

    ${customPrefix:functionName(parameter)}

Monday, March 5, 2012

1. Right click project > Properties>Javasript>Javascript libraries>Add Runtime libraries> Internet Explorer Library> ok

SpringBoot Application Event Listeners

When a spring boot application starts few events occurs in below order ApplicationStartingEvent ApplicationEnvironmentPreparedEvent Applicat...