This is default featured slide 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured slide 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

CSS Drop Down Menu

Tuesday, 14 January 2014

Struts2.x : Steps to Create Sample Project

Sample Struts2.x Program:-

[a]- Create Dynamic Project in Your Eclipse IDE.
[b]- Add Some minimum set of library in WebContent\WEB-INF\lib folder and also put it into build-path whatever above mentioned.You can download
Struts -jar from http://struts.apache.org/download.cgi

Create Sample Program

1-Create index.jsp
2-Create web.xml as deployment discriptor:-
3-Create Action Class as given below:-
Struts_Sample.java
4-Now Create struts.xml as given below:-
struts.xml
5-Create success.jsp as given below:-
success.jsp
Note :- You can create it in both either in src\struts.xml folder or WebContent\WEB-INF\classes\struts.xml like you can see in the given image below:-
Note :-
When I use eclipse to develop struts2. I found the struts.xml file can be put in src folder or WebContent\WEB-INF\classes. Both paths are ok. What's the difference? what's better? Does different path affect the performance or other thing? Is there any other path for struts.xml file? And the web.xml only can be put in WebContent\WEB-INF ?Or any other path for web.xml?
Explanation:-
1- From the web app's standpoint, there's no difference: struts.xml is expected to be at the root of the classpath after deployment. How and when it gets there is irrelevant.

2- Putting the config file in the source directory is better, because you shouldn't need to manage the class directory itself. Tools like Maven move artifacts from a project's src/main/resources directory to the classpath. It won't move an XML file from src/main/java unless specifically configured to do so.

3- No.

4- You may override the default location. There's rarely a good reason to; so don't. If you're using multiple config files it's fine to put them in packages, but I'd leave them on the classpath.

5- That's a totally unrelated question, but it should stay in the default location, WEB-INF. WebContent is project-specific (Eclipse)–you'd need to configure the deployment/packaging process to find it.

Multiple Configuration File(struts.xml)

For a large application, it is better to use multiple configuration file that one so that it may be easy to manage the application. We can create many configuration files but we need to define it in the struts.xml file. The includesub-element of struts is used to define the supported configuration file.
Example :-
1- Create index.jsp
2- Create struts.xml:-
3- Create WEB-INF/classes/first/struts-first.xml and WEB-INF/classes/second/struts_second.xml:-
[a]- Create WEB-INF/classes/first/struts-first.xml:-
[b]- Create WEB-INF/classes/second/struts_second.xml:-
4- Create Action Class For this struts-first.xml And struts_second.xml:-
[a]- Create Action Class For this struts-first.xml:-
Struts_Sample1.java:-
[b]- Create Action Class For this struts_second.xml:-
Struts_Sample2.java:-
5- Create welcome.jsp:-
6- Create error.jsp:-

Struts 2.x : Few main interceptors

Multiple Namespace:-

We can define multiple namespaces in struts.xml file by the namespace attribute of package element. As we know, default namespace is /(root). Let's see the simple example to define multiple namespaces in struts.xml file.

execAndWait interceptor:-

1- The execAndWait interceptor also known as execute and wait interceptor is used to display the intermediate result.

2- It is recommended to use for long running action.

3- It is not found in the default stack bydefault. So you need to specify it explicitely.

4- If you don't specify "wait" result, struts framework displays an intermediate result until your request is completed.

5- For the custom intermediate result, you need to define "wait" result in struts.xml file. In your page, you can display processing image
etc. So, it is better to specify the custom result.as below:-

Interface ServletRequestAware:-

All Actions that want to have access to the servlet request object must implement this interface. This interface is only relevant if the
Action is used in a servlet environment.
Note that using this interface makes the Action tied to a servlet environment, so it should be<> avoided if possible since things like unit testing will become more difficult.

FileUpload Example:-

1- Create imageUpload.jspwhere you have to upload images/file.
Snapshot of piece of code is given below:-
2-create struts.xml as snapshot is given below:-
3-create action class FileUpload.Struts2_Sample_Using_FileUpload_Interceptor as snapshot given:-
4- Create displayImage.jsp on success event occurs:- As snapshot given below:

Sunday, 5 January 2014

JDBC: 5 Steps to connect to Database



There are 5 steps to connect any java application with the database in java using JDBC. They are as follows:
  • Register the driver class
  • Creating connection
  • Creating statement
  • Executing queries
  • Closing connection
1) Register the driver class
The forName () method of Class class is used to register the driver class. This method is used to dynamically load the driver class.
Syntax of forName () method
public static void forName(String className)throws ClassNotFoundException  
Class.forName(“com.mysql.jdbc.Driver”);//For MySQL driver
The getConnection () method of DriverManager class is used to establish connection with the database.
Syntax of getConnection () method
 1) public static Connection getConnection(String url)throws SQLException  
 2) public static Connection getConnection(String url,String name,String password)  
  throws SQLException.
Connection con=DriverManager.getConnection(“jdbc:mysql://localhost:3306/dbname”,”username”,”password”);     // For MySQL driver
The createStatement () method of Connection interface is used to create statement. The object of statement is responsible to execute queries with the database.
Syntax of createStatement () method
public Statement createStatement()throws SQLException  
Statement stmt=con.createStatement ();
The executeQuery () method of Statement interface is used to execute queries to the database. This method returns the object of ResultSet that can be used to get all the records of a table.
Syntax of executeQuery () method
public ResultSet executeQuery(String sql)throws SQLException
ResultSet rs=stmt.executeQuery("select * from TableName"); 
while(rs.next()){ 
System.out.println(rs.getInt(1)+" "+rs.getString(2)); 
By closing connection object statment and ResultSet will be closed automatically. The close () method of Connection interface is used to close the connection.
Syntax of close () method
public void close()throws SQLException 
con.close();  
DriverManager Class:-
The DriverManager class acts as an interface between user and drivers. It keeps track of the drivers that are available and handles establishing a connection between a database and the appropriate driver. The DriverManager class maintains a list of Driver classes that have registered themselves by calling the method DriverManager.registerDriver ().
Connection interface:
A Connection is the session between java application and database. The Connection interface is a factory of Statement, PreparedStatement, and DatabaseMetaData i.e. object of Connection can be used to get the object of Statement and DatabaseMetaData. The Connection interface provide many methods for transaction management like commit(),rollback() etc.
By default, connection commits the changes after executing queries.
1) public Statement createStatement (): creates a statement object that can be used to execute SQL queries.
2) public Statement createStatement (int resultSetType,int resultSetConcurrency): Creates a Statement object that will generate ResultSet objects with the given type and concurrency.
3) public void setAutoCommit (boolean status): is used to set the commit status.By default it is true.
4) public void commit (): saves the changes made since the previous commit/rollback permanent.
5) public void rollback (): Drops all changes made since the previous commit/rollback.
6) public void close (): closes the connection and Releases a JDBC resources immediately.
Statement interface
The important methods of Statement interface are as follows:
1) public ResultSet executeQuery(String sql): is used to execute SELECT query. It returns the object of ResultSet.
2) public int executeUpdate (String sql): is used to execute specified query, it may be create, drop, insert, update, delete etc.
3) public boolean execute(String sql): is used to execute queries that may return multiple results.
4) public int[] executeBatch(): is used to execute batch of commands.
ResultSet interface
The object of ResultSet maintains a cursor pointing to a particular row of data. Initially, cursor points to before the first row.
By default, ResultSet object can be moved forward only and it is not updatable.
But we can make this object to move forward and backward direction by passing either TYPE_SCROLL_INSENSITIVE or TYPE_SCROLL_SENSITIVE in createStatement(int,int) method as well as we can make this object as updatable by:
Statement stmt = con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,              
ResultSet.CONCUR_UPDATABLE); 
For Example:-
package com.javacoreconcept.jeetendra;

import java.sql.Connection;
import java.sql.DriverManager;

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

public class JdbcSample {
        
            public static void main(String...jdbc) throws ClassNotFoundException, SQLException{
              Class.forName("com.mysql.jdbc.Driver");
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/hibernatesample","root","admin");
              String query="Select * from UserDetails2";
            Statement
              ResultSet rs=stmt.executeQuery(query);
              rs.absolute(5);
              System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3));
               con.close();}
}
Note:- boolean absolute( int row ) throws SQLException;

    /**
     * Moves the cursor a relative number of rows, either positive or negative.
     * Attempting to move beyond the first/last row in the
     * result set positions the cursor before/after the
     * the first/last row. Calling <code>relative(0)</code> is valid, but does
     * not change the cursor position.
PreparedStatement:
The PreparedStatement interface is a subinterface of Statement. It is used to exeucte parameterized query.
Why use PreparedStatement?
The performance of the application will be faster if you use PreparedStatement interface because query is compiled only once.

The prepareStatement() method of Connection interface is used to return the object of PreparedStatement.Syntax:
public PreparedStatement prepareStatement(String query)throws SQLException{}