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

Monday, February 13, 2012

Write File in java

private static void writeFile(String stringToWrite, String pathResponse) {
        FileWriter fw = null;
        BufferedWriter out = null;
        try {
            fw = new FileWriter(pathResponse,true);
            out = new BufferedWriter(fw);
            out.write(stringToWrite);   
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                out.close();
                fw.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
       
    }

Read a File Using JAVA

public class ReadFile {
  
    public static void main(String args[]){
        String pathRequest = "SupportFiles\\Test\\"+apiName+".txt";
        readFile(pathRequest);
    }

    private static void readFile(String pathRequest) {
        FileInputStream fis = null;
        DataInputStream in = null;
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            fis = new FileInputStream(pathRequest);
            in = new DataInputStream(fis);
            isr = new InputStreamReader(in);
            br = new BufferedReader(isr);
            String strLine;
            while ((strLine = br.readLine()) != null){
                  System.out.println (strLine);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                br.close();
                isr.close();
                in.close();
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

Sunday, February 12, 2012

SQL JOINS

  • JOIN: Return rows when there is at least one match in both tables
  • LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table
  • RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table
  • FULL JOIN: Return rows when there is a match in one of the tables

Friday, February 3, 2012

Exception Handling in JSTL

Using jstl tag <c:catch> we can capture the exceptions occuring in jsp pages



<%@ taglib  prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<html>
<head>
   <title>Error Handling Example</title>
</head>
<body>
<c:catch var ="catchException">
The exception will be thrown inside the catch:<br>
<% int x = 5/0;%>
</c:catch>

<c:if test = "${catchException!=null}">
The exception is : ${catchException}<br><br>
There is an exception: ${catchException.message}<br>
</c:if>


</body>
</html>

Using JSTL in Web Application

  1. Create Web Application. 
  2. Add standard.jar and jstl.jar in lib folder under WEB-INF.
  3. Add a taglib directive to the jsp page on which you want to use JSTL as
    <%@ taglib  prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
  4. The page is now configured to use JSTL library


    Example is:-


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib  prefix="c" uri="http://java.sun.com/jstl/core_rt" %>
<!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>Enter credentials to Login</title>
</head>
<body>
Values is <c:out value="${param.userType}" /> here
</body>
</html>

How to add style sheet to an html page

How to add style sheet to an html page

How to add style sheet to an html page

  • Create a css file say index.css and add style to it
body{
@import url('https://fonts.googleapis.com/css?family=Tangerine&display=swap');
body{
font-family: 'Tangerine', serif;
    font-size: 48px;
}
  • In the header section of your html page, add the reference to the above css file path 
<!DOCTYPE html>
<html lang="en">
<header>
<link rel="stylesheet" href="index.css">
</header>
<body>
<div>your page content</div>
</body>
  • The style defined in the index.css file will be available on the page




  • Please do like and follow our facebook page here
  • For more queries, feel free to e-mail us here
  • Just to let you know, we are an Amazon Associate. To help and support us, you can shop for different products available on Amazon from here

SpringBoot Application Event Listeners

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