Saturday, 28 September 2013

How to Disable Anchor tag after clik the link Using JQuery

<html>
<head>
<style>
.myDisableClass {
pointer-events: none;
cursor: default;
text-decoration: none;
}
</style>
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#firstAnchor").click(function(e) {
$(this).addClass("myDisableClass");
});
});
</script>
</head>
<body>
<a id="firstAnchor" href="https://www.google.com" target="#">Click Me</a>
</body>
</html>

Friday, 27 September 2013

JDBC CURD Operations using PreparedStatement


//Insert Record into table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="insert into emp values(?,?)";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,25);
pstmt.setString(2,"jamesGosling");
pstmt.executeUpdate();

System.out.println("+++Record inserted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}


//Update Record/Records in the table using PreparesStatement in JDBC


package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="update emp set name=? where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setString(1,"java");
pstmt.setInt(2,25);

pstmt.executeUpdate();

System.out.println("+++Record updated successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}


//Delete Record/Records in the table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="delete from emp where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,25);

int count=pstmt.executeUpdate();

System.out.println("+++"+count+" Records deleted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}



//Retrieve Record/Records in the table using PreparesStatement in JDBC

package com.iton;

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

public class MainApp2 {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

String query="select * from emp where no=?";
PreparedStatement pstmt=con.prepareStatement(query);

pstmt.setInt(1,26);

ResultSet rs=pstmt.executeQuery();

while(rs.next()) {
System.out.print("No:"+rs.getInt("no"));
System.out.println(" Name:"+rs.getString("name"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

JDBC CURD operations using Statement object


//Create table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "create table emp(no int,name varchar(15))";

stmt.executeUpdate(query);
System.out.println("+++Table is created successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Insert Record into table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "insert into emp values(25,'jamesGosling')";

stmt.executeUpdate(query);
System.out.println("+++Record inserted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Update Record/Records in the table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "update emp set name='james' where no=25";

stmt.executeUpdate(query);
System.out.println("+++Record updated successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Delete Record/Records in the table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "delete from emp where no=25";

int count=stmt.executeUpdate(query);
System.out.println("+++"+count+" Records deleted successfully+++");

} catch (SQLException e) {
e.printStackTrace();
}
}
}

//Retrieve Record/Records in the table using Statement object in JDBC

package com.iton;

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

public class MainApp {

public static void main(String[] args) {

try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/ramakrishna","root","root");

Statement stmt = con.createStatement();
String query = "select * from emp";

ResultSet rs=stmt.executeQuery(query);

while(rs.next()) {
System.out.print("No:"+rs.getInt("no"));
System.out.println(" Name:"+rs.getString("name"));
}

} catch (SQLException e) {
e.printStackTrace();
}
}
}

Friday, 20 September 2013

WebServices in Java Introduction

Distributed Computing Technology:

   ->“Distributed computing” is all about designing and building applications as a
    set of processes that are distributed across a network.

WEB SERVICES:

   ->To achive something called as Distributed computing technology based applications,
    Webservices is the one of the best technology

   ->It is a Web API described in WSDL (Web Service Description Language),
    and Web services are usually self-contained and self-describing.

   ->Web Services can be accessed by any language, using any component model,
    running on any operating system.

   ->Web service is a way of communication that allows interoperability between
    different applications on different platforms,

    for example, a java based application on Windows can communicate with a .Net based
    one on Linux.

   ->The communication can be done through a set of XML messages over HTTP protocol.

   ->web services are language independent,browser independent,platform independent,

    so once we convert our application(java or .net or other) into web services,
    then other applications can communicate with our application

   ->By using web services we can re use the code.

    For Example,One project is developed for one company,same company requires
    another project that is developed in .net,

    After that some data is required to transfer from one project to other,in this
    situation also we can use web services

   ->Web service system dedicated for supporting machine-to-machine transactions
    over a network.

   ->Web services can be discovered using UDDI (Universal Description,
    Discovery and Integration) protocol.

    By exchanging SOAP (Simple Object Access Protocol) messages typically over
    HTTP (with XML),other systems can interact with Web services.

   ->In java To develope webservices we can use two ways

    1)SOAP protocolo based (Simple Object Access Protocol) by using JAX-WS
    (Java API for XML Webservices) API

    2)REST architecture based (Representation State Transfer) by using JAX-RS
    (Java API for Restful WebServices) API

Tuesday, 17 September 2013

Spring web.xml with Listener,Without LIstener

Using Two configuration files with Listener

  • Eventhough we provided the name of applicationContext.xml,It will search for spring-servlet.xml,Here servlet-name is spring

  • ContextLoaderListener,it will get the configuration file(using contextConfigLocation) and create Spring contatiner object when the project is deployed into the server

  • Dispatcher servlet creating container object with another configuration file(xxx-servlet.xml)

  • Here xxx means spring(<servlet-name>spring</servlet-name>)


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>



    Using only one configuration file with Listener

  • Here Listener and DispatcherServlet both using spring-servlet.xml


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/spring-servlet.xml</param-value>
    </context-param>
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    </web-app>


    Using One Configuration file without Listener

  • We can use init parameter also for giving configuration file name,this init-param is under DispatcherServlet

  • we no need to follow particular name(xxx-servlet.xml),we no need to place that configuration file in WEB-INF folder also

  • but in this way Spring Container object is created when the DispatcherServlet object is created


  • <web-app>
    <servlet>
    <servlet-name>spring</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/Config/Congig.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
    <servlet-name>spring</servlet-name>
    <url-pattern>/</url-pattern>
    </servlet-mapping>
    </web-app>

    Saturday, 7 September 2013

    Working with Date class in java,Converting String to Date


    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;

    //This examples are used to take date in string format and stored in date object
    public class DateExamples {


    public static void main(String[] args) {

    System.out.println("+++++++++++++++++++++++++++++++++");
    //Just displaying today date in required format
    DateFormat df = new SimpleDateFormat("dd/yy/MM");
    String formattedDate = df.format(new Date());
    System.out.println("TO DAY DATE IS:"+formattedDate);
    System.out.println("+++++++++++++++++++++++++++++++++");
    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter = new SimpleDateFormat
    ("dd-MMM-yyyy");
    String dateInString = "7-Jun-2013";

    try {

    Date date = formatter.parse(dateInString);
    System.out.println(date);
    System.out.println(formatter.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter1 = new SimpleDateFormat
    ("dd/MM/yyyy");
    String dateInString1 = "07/06/2013";

    try {

    Date date = formatter1.parse(dateInString1);
    System.out.println(date);
    System.out.println(formatter1.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter2 = new SimpleDateFormat
    ("MMM dd, yyyy");
    String dateInString2 = "Jun 7, 2013";

    try {

    Date date = formatter2.parse(dateInString2);
    System.out.println(date);
    System.out.println(formatter2.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter3 = new SimpleDateFormat
    ("E, MMM dd yyyy");
    String dateInString3 = "Fri, June 7 2013";

    try {

    Date date = formatter3.parse(dateInString3);
    System.out.println(date);
    System.out.println(formatter3.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");


    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the date
    SimpleDateFormat formatter4 = new SimpleDateFormat
    ("EEEE, MMM dd, yyyy HH:mm:ss a");
    String dateInString4 = "Friday, Jun 7, 2013 12:10:56 PM";

    try {

    Date date = formatter4.parse(dateInString4);
    System.out.println(date);
    System.out.println(formatter4.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //In this format only we have to give the time
    SimpleDateFormat formatter5 = new SimpleDateFormat
    ("HH:mm:ss a");
    String dateInString5 = "12:10:56 PM";

    try {

    Date date = formatter5.parse(dateInString5);
    //System.out.println(date);
    System.out.println(formatter5.format(date));

    } catch (ParseException e) {
    e.printStackTrace();
    }
    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //It just displaying current date and time in required format
    SimpleDateFormat formatter6 = new SimpleDateFormat
    ("yyyy/MM/dd EEE,hh:mm::ss a");
    System.out.println(formatter6.format(new Date()));

    System.out.println("+++++++++++++++++++++++++++++++++");

    System.out.println("+++++++++++++++++++++++++++++++++");
    //It just displaying current date and time in required format
    SimpleDateFormat formatter7 = new SimpleDateFormat
    ("yyyy/MMMM/dd EEE,hh:mm::ss a");
    System.out.println(formatter7.format(new Date()));

    System.out.println("+++++++++++++++++++++++++++++++++");


    }

    }
    OUTPUT:
    +++++++++++++++++++++++++++++++++
    TO DAY DATE IS:07/13/09
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    07-Jun-2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    07/06/2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    Jun 07, 2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 00:00:00 IST 2013
    Fri, Jun 07 2013
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    Fri Jun 07 12:10:56 IST 2013
    Friday, Jun 07, 2013 12:10:56 PM
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    12:10:56 PM
    ++++++++++++++++++++++++++++++++++
    ++++++++++++++++++++++++++++++++++
    2013/09/07 Sat,03:33::32 PM
    +++++++++++++++++++++++++++++++++
    +++++++++++++++++++++++++++++++++
    2013/September/07 Sat,03:33::32 PM
    +++++++++++++++++++++++++++++++++

    Spring MVC working Flow

  • By using URL/From jsp we are sending request

  • This request will come to web.xml file and execute DispatcherServlet

  • It is the responsibility of DispatcherServlet to identify perticular HandlerMappings

  • By using HandlerMapping DispathcerServlet will search for requested url path matching in spring configuration file/Spring Configuration class

  • If it is available,DispatcherServlet will call methods in our Controller

  • Controller process the request,get the data form form,store into pojo class/javabean,if required perform validations

  • And then Controller returns ModelAndView object with data/errors and jsp name

  • DispatcherServlet take the ModleAndView object and stored in scoped variables

  • Based on the jsp name returned by Controller,DispatcherServlet will find out jsp by using ViewResolver(InternalResourceViewResolver)

  • Finally loginpage/successpage/failure page send to clietnt/jsp/other

  • By using scoped variables we can display the data/errors