Thursday, December 30, 2010

Delete Row from Table


Before deleting row

DELETE FROM table1 WHERE 
first_name='akshay';

select * from table1;

Output---->
After deleting row

Insert value into table

This summary is not available. Please click here to view the post.

Setting for JSP program

A development environment is where you would develop your JSP programs, test them and finally run them.
This tutorial will guide you to setup your JSP development environment which involves following steps:

Setting up Java Development Kit

This step involves downloading an implementation of the Java Software Development Kit (SDK) and setting up PATH environment variable appropriately.
You can downloaded SDK from Oracle's Java site: Java SE Downloads.
Once you download your Java implementation, follow the given instructions to install and configure the setup. Finally set PATH and JAVA_HOME environment variables to refer to the directory that contains java and javac, typically java_install_dir/bin and java_install_dir respectively.
If you are running Windows and installed the SDK in C:\jdk1.5.0_20, you would put the following line in your C:\autoexec.bat file.

set PATH=C:\jdk1.5.0_20\bin;%PATH%
set JAVA_HOME=C:\jdk1.5.0_20 
Alternatively, on Windows NT/2000/XP, you could also right-click on My Computer, select Properties, then Advanced, then Environment Variables. Then, you would update the PATH value and press the OK button.
On Unix (Solaris, Linux, etc.), if the SDK is installed in /usr/local/jdk1.5.0_20 and you use the C shell, you would put the following into your .cshrc file.

setenv PATH /usr/local/jdk1.5.0_20/bin:$PATH
setenv JAVA_HOME /usr/local/jdk1.5.0_20
Alternatively, if you use an Integrated Development Environment (IDE) like Borland JBuilder, Eclipse, IntelliJ IDEA, or Sun ONE Studio, compile and run a simple program to confirm that the IDE knows where you installed Java.

Setting up Web Server: Tomcat

A number of Web Servers that support JavaServer Pages and Servlets development are available in the market. Some web servers are freely downloadable and Tomcat is one of them.
Apache Tomcat is an open source software implementation of the JavaServer Pages and Servlet technologies and can act as a standalone server for testing JSP and Servlets and can be integrated with the Apache Web Server. Here are the steps to setup Tomcat on your machine:
  • Download latest version of Tomcat from http://tomcat.apache.org/.
  • Once you downloaded the installation, unpack the binary distribution into a convenient location. For example in C:\apache-tomcat-5.5.29 on windows, or /usr/local/apache-tomcat-5.5.29 on Linux/Unix and create CATALINA_HOME environment variable pointing to these locations.
Tomcat can be started by executing the following commands on windows machine:
%CATALINA_HOME%\bin\startup.bat
 
 or
 
 C:\apache-tomcat-5.5.29\bin\startup.bat
Tomcat can be started by executing the following commands on Unix (Solaris, Linux, etc.) machine:
$CATALINA_HOME/bin/startup.sh
 
or
 
/usr/local/apache-tomcat-5.5.29/bin/startup.sh
After a successful startup, the default web applications included with Tomcat will be available by visiting http://localhost:8080/. If everything is fine then it should display following result:


Tomcat Home page
Further information about configuring and running Tomcat can be found in the documentation included here, as well as on the Tomcat web site: http://tomcat.apache.org
Tomcat can be stopped by executing the following commands on windows machine:
%CATALINA_HOME%\bin\shutdown
or

C:\apache-tomcat-5.5.29\bin\shutdown
Tomcat can be stopped by executing the following commands on Unix (Solaris, Linux, etc.) machine:
$CATALINA_HOME/bin/shutdown.sh

or

/usr/local/apache-tomcat-5.5.29/bin/shutdown.sh

Setting up CLASSPATH

Since servlets are not part of the Java Platform, Standard Edition, you must identify the servlet classes to the compiler.
If you are running Windows, you need to put the following lines in your C:\autoexec.bat file.

set CATALINA=C:\apache-tomcat-5.5.29
set CLASSPATH=%CATALINA%\common\lib\jsp-api.jar;%CLASSPATH% 
Alternatively, on Windows NT/2000/XP, you could also right-click on My Computer, select Properties, then Advanced, then Environment Variables. Then, you would update the CLASSPATH value and press the OK button.
On Unix (Solaris, Linux, etc.), if you are using the C shell, you would put the following lines into your .cshrc file.
setenv CATALINA=/usr/local/apache-tomcat-5.5.29
setenv CLASSPATH $CATALINA/common/lib/jsp-api.jar:$CLASSPATH 
NOTE: Assuming that your development directory is C:\JSPDev (Windows) or /usr/JSPDev (Unix) then you would need to add these directories as well in CLASSPATH in similar way as you have added above.

JavaServer Pages

What is JavaServer Pages?

JavaServer Pages (JSP) is a technology for developing web pages that support dynamic content which helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>.

A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands.

Using JSP, you can collect input from users through web page forms, present records from a database or another source, and create web pages dynamically.
JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages and sharing information between requests, pages etc.

Why Use JSP?

JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offer several advantages in comparison with the CGI.
  • Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having a separate CGI files.
  • JSP are always compiled before it's processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested.
  • JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP etc.
  • JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines.
Finally, JSP is an integral part of J2EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding.

Advantages of JSP:

Following is the list of other advantages of using JSP over other technologies:
  • vs. Active Server Pages (ASP): The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers.
  • vs. Pure Servlets: It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML.
  • vs. Server-Side Includes (SSI): SSI is really only intended for simple inclusions, not for "real" programs that use form data, make database connections, and the like.
  • vs. JavaScript: JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc.
  • vs. Static HTML: Regular HTML, of course, cannot contain dynamic information.

    Tuesday, December 28, 2010

    Create Table

    
    create table table1
    (
       first_name char(30),
       last_name char(30),
       contact_no int,
       email_id varchar(20)
    );
    
    select * from table1;
    
    
    Output--->

    Create Database

    create database alertclub;

    Create a Frame

    import javax.swing.*;
    
    class Frame1 extends JFrame
    {
        public static void main(String s[])
        {
    
            JFrame frame=new JFrame();
            frame.setSize(200,200);
            frame.setVisible(true);
            frame.setDefaultCloseOperation(EXIT_ON_CLOSE);      
        }
    
    }
     
    Output
    
    

    Sunday, December 26, 2010

    JDBC


    What is the JDBC?
    Java Database Connectivity (JDBC) is a standard Java API to interact with relational databases form Java. JDBC has set of classes and interfaces which can use from Java application and talk to database without learning RDBMS details and using Database Specific JDBC Drivers.
    Features of JDBC 4.0
    The major features added in JDBC 4.0 include :
    • Auto-loading of JDBC driver class.
    • Connection management enhancements.
    • Support for RowId SQL typeDataSet implementation of SQL using Annotations.
    •  SQL exception handling enhancements .
    • SQL XML support.
    Basic Steps in writing a Java program using JDBC
    JDBC makes the interaction with RDBMS simple and intuitive. When a Java application needs to access database : 
    • Load the RDBMS specific JDBC driver because this driver actually communicates with the database (Incase of JDBC 4.0 this is automatically loaded). 
      • driver = "sun.jdbc.odbc.JdbcOdbcDriver";
      • conn=DriverManager.getConnection(url,username,password);
    • Open the connection to database which is then used to send SQL statements and get results back. 
    • Create JDBC Statement object. This object contains SQL query.
      • PreparedStatement stmt=conn.prepareStatement("insert into login values(?,?,?)");
    • Execute statement which returns resultset(s). ResultSet contains the tuples of database table as a result of SQL query.
      •  stmt.executeUpdate();
    • Process the result set.  
    • Close the connection.
      • con.close();

      JDBC Architecture.
      The JDBC Architecture consists of two layers: 
      • The JDBC API, which provides the application-to-JDBC Manager connection. 
      • The JDBC Driver API, which supports the JDBC Manager-to-Driver Connection.
      The JDBC API uses a driver manager and database-specific drivers to provide transparent connectivity to heterogeneous databases. The JDBC driver manager ensures that the correct driver is used to access each data source. The driver manager is capable of supporting multiple concurrent drivers connected to multiple heterogeneous databases. The location of the driver manager with respect to the JDBC drivers and the Java application is shown in Figure 1.

      Figure 1: JDBC Architecture



      Main components of JDBC
      The life cycle of a servlet consists of the following phases:
      • DriverManager: Manages a list of database drivers. Matches connection requests from the java application with the proper database driver using communication subprotocol. The first driver that recognizes a certain subprotocol under JDBC will be used to establish a database Connection.
        •  conn=DriverManager.getConnection(url,username,password);
      • Driver: The database communications link, handling all communication with the database. Normally, once the driver is loaded, the developer need not call it explicitly.
        • driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        • Class.forName(driver);
      • Connection : Interface with all methods for contacting a database.The connection object represents communication context, i.e., all communication with database is through connection object only. 
        • getConnection(url,username,password);
      • Statement : Encapsulates an SQL statement which is passed to the database to be parsed, compiled, planned and executed.
        • stmt.executeUpdate();
      • ResultSet: The ResultSet represents set of rows retrieved due to query execution.
        • ResultSet rs;
        • rs=stmt.executeQuery("SELECT * FROM login");


      JDBC application works
      A JDBC application can be logically divided into two layers:
      1. Driver layer
      2. Application layer
      • Driver layer consists of DriverManager class and the available JDBC drivers.
      • The application begins with requesting the DriverManager for the connection.
      • An appropriate driver is choosen and is used for establishing the connection. This connection is given to the application which falls under the application layer.
      • The application uses this connection to create Statement kind of objects, through which SQL commands are sent to backend and obtain the results.

      Figure 2: JDBC Application

      JDBC drivers are divided into four types or levels. 
      The different types of jdbc drivers are:
      • Type 1: JDBC-ODBC Bridge driver (Bridge) 
      • Type 2: Native-API/partly Java driver (Native) 
      • Type 3: AllJava/Net-protocol driver (Middleware) 
      • Type 4: All Java/Native-protocol driver (Pure)

      For More Details of Driver Please Click Here...

      JSP Life Cycle

      JSP LifeCycle
      JSP never outputs the content directly to the browser. Instead, JSP relies on initial server-side processing process which translates JSP page into the JSP Page class. The page class then will handle all the requests made of JSP. The JSP life cycle is described as the picture below:

      JSP Life Cycle

      JSP life cycle can be divided into four phases: Translation, Initialization, execution and finalization.
      Translation

      In the translation phase, JSP engine checks for the JSP syntax and translate JSP page into its page implementation class if the syntax is correct. This class is actually a standard Java servlet. After that, JSP engine compiles the source file into class file and ready for use.

      If the container receives the request, it checks for the changes of the JSP page since it was last translated. If no changes was made, It just loads the servlet otherwise the process of check, translate and compile occurs again. Because the compilation process takes time so JSP engine wants to minimize it to increase the performance of the page processing.
      Initialization

      After the translation phase, JSP engine loads the class file and create an instance of of the servlet to handle processing of the initial request. JSP engines will call a method jspInit() to initialize the a servlet. jspInit method is generated during the translation phase which is normally used for initializing application-level parameters and resources. You can also overide this method by using declaration.

      <%!
      public void jspInit(){
      // put your custom code here
      }
      %>



      After the initialization phase, the web container calls the method _jspService() to handle the request and returning a response to the client. Each request is handled is a separated thread. Be noted that all the scriptlets and expressions end up inside this method. The JSP directives and declaration are applied to the entire page so the are outside of this method.
      Finalization

      In the finalization phase, the web container calls the method jspDestroy(). This method is used to clean up memory and resources. Like jspInit() method, you can override the jspDestroy() method also to do your all clean up such as release the resources you loaded in the initialization phase....

      <%!
      public void jspDestroy(){
      // put your custom code here
      // to clean up resources
      }
      %>

      JSP



      JSP stands for JavaServer Pages. JSP is one of the most powerful, easy-to-use and fundamental technology for Java web developers. JSP combines HTML, XML, Java Servlet and JavaBeans technologies into one highly productive technology to allow web developers to develop reliable, high performance and platform independent web applications and dynamic websites.


      JSP Advantages


      • Separate the business logic and presentation: The logic to generate dynamic elements or content is implemented and encapsulated by using JavaBeans components. The user interface (UI) is created by using special JSP tags. This allows developers and web designers to maintain the JSP pages easily. 
      •  Write Once, Run Anywhere: as a part of Java technology, JPS allows developers to developer JSP pages and deploy them in a variety of platforms, across the web servers without rewriting or changes.
      •  Dynamic elements or content produced in JSP can be served in a different formats: With JSP you can write web application for web browser serving HTML format. You can even produce WML format to serve hand-held device browsers. There is no limitation of content format which JSP provides.
      •  Take advantages of Servlet API: JSP technically is a high-level abstraction of Java Servlets. It is now easier to get anything you've done with Servlet by using JSP. Beside that you can also reuse all of your Servlets you've developed so far in the new JSP.