Tuesday, July 23, 2013

Hibernate SessionFactory buildSessionFactory deprecated fix


Fix for Hibernate SessionFactory buildSessionFactory deprecation as of Hibernate 4.0 

private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;

private static SessionFactory configureSessionFactory() throws HibernateException {
   Configuration configuration = new Configuration();
   configuration.configure();
   serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();        
   sessionFactory = configuration.buildSessionFactory(serviceRegistry);
   return sessionFactory;
}



Generic JDBC Program for Derby


This is a simple JDBC Program for Derby database. No standards followed, you can see that connection is not closed, statement is not cleared.

Can be used to check database connectivity.

SQL Queries
create table test (id integer, name varchar(10), age integer, address varchar(30));

insert into test values (1, 'test1', 20, '80 Absolute Avenue');
insert into test values (2, 'test2', 30, '90 Absolute Avenue');

select * from test



package com.hibernate.demo;

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

public class GenericJDBC {

public static void main(String[] args) {
// TODO Auto-generated method stub
String url ="jdbc:derby://localhost:1527/sample;create=true";

Connection conn;

try
{

Class.forName("org.apache.derby.jdbc.ClientDriver");

conn = DriverManager.getConnection(url, "user", "user");

Statement st = conn.createStatement();

ResultSet rs = st.executeQuery("select * from test");

while(rs.next())
{
System.out.println(rs.getInt(1)
+" " + rs.getString(2) +
" " + rs.getInt(3)+ " "+
rs.getString(4));
}

}catch(Exception e)
{
e.printStackTrace();
}
finally
{

}

}

}

Wednesday, July 17, 2013

ANT Weblogic WebApp Sample File

This article contains the Weblogic Web Application Ant Sample deployment script.

Very useful for configuration and deployment.

<project name="AppBuilder" default="deploy" basedir="."> 
        <property file="build.properties"/> 
<!-- 
Tasks to be done 
1. clean 
2. init 
3. compile 
4. copy 
5. war 
 --> 
        <path id="classpath"> 
                <!--<fileset dir="${lib.dir}" includes="*.jar"/>--> 
                <fileset dir="common" includes="*.jar"/> 
        </path> 

        <target name="clean"> 
                <echo>Cleaning the ${build.dir} and ${dist.dir}</echo> 
                <delete dir="${build.dir}"/> 
                <delete dir="${dist.dir}"/> 
        </target> 
        <target name="init" depends="clean"> 
                <echo>Creating the required directories</echo> 
                <mkdir dir="${dist.dir}"/> 
                <mkdir dir="${build.dir}\WEB-INF\classes"/>                 
                <mkdir dir="${build.dir}\WEB-INF\lib"/> 
        </target> 

        <target name="compile" depends="init"> 
                <echo>Compile the source files</echo> 
                <javac srcdir="${src.dir}" destdir="${build.dir}\WEB-INF\classes"> 
                        <classpath refid="classpath"/> 
                </javac> 
        </target>         
        
        <target name="copy" depends="compile"> 
                <copy todir="${build.dir}"> 
                        <fileset dir="${web.content}"/> 
                </copy> 
                <copy todir="${build.dir}/WEB-INF/classes"> 
                        <fileset dir="${src.dir}" includes="**/*.properties, **/*.xml, **/*.wsdl"/> 
                </copy> 
        </target>                 

        <target name="war" depends="copy"> 
                <echo>Building the war file</echo> 
                <war destfile="${dist.dir}\${project.name}.war" webxml="${build.dir}\WEB-INF\web.xml"> 
                        <fileset dir="${build.dir}"/> 
                </war> 
        </target> 

        <taskdef name="wldeploy" classname="weblogic.ant.taskdefs.management.WLDeploy"> 
          <classpath> 
          <pathelement location="common/weblogic.jar"/> 
          </classpath> 
        </taskdef> 

        <target name="deploy"> 
           <wldeploy action="deploy" 
                  source="dist/SMDBWebservice.war" name="SMDBWebservice" 
                  user="weblogic" password="weblogic" verbose="true" adminurl="t3://localhost:7001" 
                  debug="true" targets="AdminServer"/> 
        </target> 

        <target name="undeploy"> 
           <wldeploy action="undeploy" name="SMDBWebservice" 
                  user="weblogic" password="weblogic" verbose="true" adminurl="t3://localhost:7001" 
                  debug="true" targets="AdminServer"/> 
        </target> 

        <target name="redeploy"> 
           <wldeploy action="redeploy" 
                  source="dist/SMDBWebservice.war" name="SMDBWebservice" 
                  user="weblogic" password="weblogic" verbose="true" 
                  adminurl="t3://localhost:7001" 
                  debug="true" targets="AdminServer"> 
                 <!--<files file="index.jsp"/>--> 
           </wldeploy> 
        </target> 

        <taskdef name="wlconfig" classname="weblogic.ant.taskdefs.management.WLConfig"> 
          <classpath> 
          <pathelement location="common/weblogic.jar"/> 
          </classpath> 
        </taskdef> 

        <taskdef name="wlserver" classname="weblogic.ant.taskdefs.management.WLServer"> 
          <classpath> 
          <pathelement location="common/weblogic.jar"/> 
          <pathelement location="common/ant-weblogic.jar"/> 
          </classpath> 
        </taskdef> 

<target name="JDBCPool"> 
                <wlconfig url="t3://localhost:7001" username="weblogic" password="weblogic"> 
                        
                        <query domain="base_domain" type="Server" name="AdminServer"  property="server"/> 
                <create type="JDBCConnectionPool" 
                         name="CMSPool"       
                         property="cmspool">       
                         <set attribute="CapacityIncrement" value="1"/>       
                         <set attribute="DriverName"         
                                         value="com.ibm.db2.jcc.DB2Driver"/>       
                         <set attribute="InitialCapacity" value="1"/>       
                         <set attribute="MaxCapacity" value="10"/>       
                         <set attribute="Password" value="smdbuat"/>       
                         <set attribute="Properties" value="user=dsmdd1d;SecurityMechanism=EncryptedUIDPassword;databaseName=DPSMSSMD;serverName=160.254.49.155;batchPerformanceWorkaround=true;portNumber=3600;"/>       
                         <set attribute="RefreshMinutes" value="0"/>       
                         <set attribute="ShrinkPeriodMinutes" value="15"/>       
                         <set attribute="ShrinkingEnabled" value="true"/>       
                         <set attribute="TestConnectionsOnRelease" value="false"/>       
                         <set attribute="TestConnectionsOnReserve" value="false"/>       
                         <set attribute="URL" value="jdbc:db2://160.254.49.155:3600/DPSMSSMD"/>       
                         <set attribute="Targets" value="${server}"/>   
                 </create> 

                 <create type="JDBCTxDataSource" name="CMSDataSource"> 
                        <set attribute="JNDIName" value="JDBCIBM2"/> 
                        <set attribute="PoolName" value="CMSPool"/> 
                        <set attribute="Targets" value="${server}"/> 
                 </create> 
        </wlconfig> 
</target> 

</project> 

Hello World with Apache Ant

Apache Ant (Another Neat Tool) is a Java library and Command line utility useful for automating batch code build process.

For example, let say there is Java Class file HelloWorld.java   you need to compile, jar, move and run. In command you will perform the following activities

To Compile 

javac HelloWorld.java

To Create the jar file 

jar -cvf HelloWorld.jar HelloWorld.class

or

jar -cvf HelloWorld.jar HelloWorld.class jar -cvf HelloWorld.jar HelloWorld.java

To Move the program 

move HelloWorld.jar HelloWorld1.jar

To run the program

java -cp HelloWorld.jar HelloWorld


Executing all the above steps is ok for one time, lets say you have to do 100 times in a month. This is troublesome and can be automated.

Lets see how this can be automated

Step 1: Download the Apache Ant Latest Stable Version

http://ant.apache.org



Step 2:  Extract the Zip file

Step 3:  Go to Environment Variables


Step 4:  Create variable ANT_HOME 


Step 5: Add ANT_HOME\bin in the path 


Step 6:  Open Command prompt and execute the command ant -version



Step 7:  Create the build.xml  


1. target element with name attribute 

<target name="clean"> 

Create target for each of your activities example, clean, compile, move, jar, execute etc., 

2.  Echo Command 

    <echo>Cleaning the directories</echo> 

The echo command is useful while running the ant, it show which step it is currently in. Basically it is just print command.

3. Depends Attribute

        <target name="init" depends="clean"> 

Depends is a very useful command which will execute the depends target before executing the current target. It is like parent child relationship.

4. Console Commands

                <javac srcdir="${currentfolder}\src" destdir="${currentfolder}\classes"> 
                        <classpath refid="classpath"/> 
                </javac> 

For most of the generic commands you have Ant Task defined, tasks such as Javac, Jar, War etc., 

5.  Project Root Element and Attributes

<project name="HelloWorld" default="execute" basedir="."> 

The project element defines the project name the default target it should execute and the base directory from where it should run. 

Sample build.xml file as below,

<project name="HelloWorld" default="execute" basedir="."> 
        <!-- <property file="build.properties"/> -->
<!-- Tasks to be done 1. clean  2. init  3. compile  4. jar 5. move 6.execute --> 
 
<property name="currentfolder" location="C:\Temp"/>
 
        <path id="classpath"> 
                <!--<fileset dir="${lib.dir}" includes="*.jar"/>--> 
        </path> 
<target name="clean"> 
                <echo>Cleaning the ${build.dir} and ${dist.dir}</echo> 
                <delete dir="${currentfolder}\classes"/> 
                <delete dir="${currentfolder}\lib"/> 
        </target> 
        <target name="init" depends="clean"> 
                <echo>Creating the required directories</echo> 
                <mkdir dir="${currentfolder}\classes"/> 
                <mkdir dir="${currentfolder}\lib"/> 
        </target> 

        <target name="compile" depends="init"> 
                <echo>Compile the source files</echo> 
                <javac srcdir="${currentfolder}\src" destdir="${currentfolder}\classes"> 
                        <classpath refid="classpath"/> 
                </javac> 
        </target>

<target name="jar" depends="compile">
<echo>Creating the jar file</echo> 
<jar jarfile="${currentfolder}\lib\HelloWorld.jar" basedir="${currentfolder}\classes"/>
</target>
        <target name="move" depends="jar"> 
<echo>Moving the HelloWorld.jar file</echo> 
<move file="${currentfolder}\lib\HelloWorld.jar" tofile="${currentfolder}\lib\HelloWorld1.jar"/>
        </target>            
        <target name="execute" depends="move"> 
                <echo>Executing Hello World</echo> 
<java  fork="true"
  failonerror="true"
  maxmemory="128m"
  classname="HelloWorld">
<arg value="-h"/>
<classpath>
  <pathelement location="${currentfolder}\lib\HelloWorld1.jar"/>
  <pathelement path="${java.class.path}"/>
</classpath>
</java>
        </target> 
</project> 



Step 8:  Go to Command and run ant where you have the build.xml




This is just tip of an Ice Berg, you can perform lot of activities like deploying code to the server, creating war files, create ear files, creating stubs, skeletons, ftp etc., As long you can perform some operation in command line you can perform the same using ant but in a sequence. 


Tuesday, April 30, 2013

ADB is Not Recognized as an internal or external command Fix


ADB is Not Recognized as an internal or external command Fix


Some of you are facing problems with Path, because of that you will see the error as below. Even though it is basic lot of people asking this same question. Hence posting this tutorial to fix this error.

In Windows --> Click Start --> Run --> cmd

When you run adb in the command prompt you will see an error as below

Microsoft Windows [Version 6.2.9200]
(c) 2012 Microsoft Corporation. All rights reserved.

C:\Users\MOHAMMED>adb
'adb' is not recognized as an internal or external command,
operable program or batch file.


To fix this error you need the Android SDK if you don't have it please follow the steps below,


Open the browser and navigate to http://developer.android.com/index.html


Click the Get the SDK as shown in the above image


Click Download the SDK as shown in the above image


Check the checkbox for the license agreement, Select 32 or 64 bit based on your operating systtem.
Click the download SDK link.

Wait patiently until the download is complete.

My machine is a 64 bit Windows, hence the file downloaded is adt-bundle-windows-x86_64-20130219.zip

The zip file contains the following folders eclipse & sdk.

I have extracted to the following location in my machine

Hence i have the following in the path 

C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\platform-tools


C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\tools


You can see that adb.exe is in the location C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\platform-tools



Execute the following commands in the command prompt and run adb it will be recognized now.

set ANDROID_PLATFORM_TOOLS="C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\platform-tools"

set ANDROID_TOOLS="C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\tools"

set path=%path%;%ANDROID_PLATFORM_TOOLS%;%ANDROID_TOOLS%



To Fix it permanently set in the environment variable 


Right Click My Computer --> Click Properties 


Click Advanced System Settings 

Click Environment Variables


Click New  as below, 


Variable name: ANDROID_PLATFORM_TOOLS

Variable Value: C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\platform-tools


Variable name: ANDROID_TOOLS

Variable Value: C:\Users\MOHAMMED\Downloads\adt-bundle-windows-x86_64\sdk\tools



Edit the Path variable if it exist, else create a new variable Path 

Variable name: Path

Variable Value: %Path%;%ANDROID_PLATFORM_TOOLS%;%ANDROID_TOOLS%



adb should be working on command prompt and 'adb' is not recognized as an internal or external command,
operable program or batch file is resolved now.





Friday, March 15, 2013

Enabling Developer Options in Android 4.2.1 for Galaxy Nexus or Nexus 7

Enabling Developer Options in Android 4.2.1 for Galaxy Nexus or Nexus 7

In Android 4.2.1 the developer options in the Galaxy Nexus and Nexus 7 will not be visible by default as in the below image.


The following are the steps to enable the developer options for Galaxy Nexus or Nexus 7 for Android 4.2.1

Step1:  Click on the View All Apps Dashboard



Step2:  Click on the Settings 



Step3: Click on About Phone 




Step4: Click on the Build number for 7 Times (Tap on the Build number for 7 Times)




Step5: Congrats you will see a message as below, (You are now a developer!)


Step6: You can see the developer options in the settings screen as below




Sunday, August 19, 2012

nexus7 android development

nexus7 android development 

I had problems with nexus7 and i don't found any useful article in the web. Hence thought of writing this blog for Android brothers and sister.

Here you go, if you are planning to deploy your applications in nexus7 for testing in Windows. The following is the process.

Setup the Nexus7 Settings

1. Unlock the main screen

2. Click on the View APPS Menu as highlighted below

3. Click on the settings in the APPS menu page (See the highlighted settings)



4. Click on the Device - Storage as highlighted below


5. Click on the view more options in the storage as highlighted in yellow below


6. Click on the USB Computer connection

7. Select the option Camera(PTP) option.
Your android device can be deducted only if this mode is selected.

Don't choose the option Media device (MTP) which is highly supported in Mac OS.


Enable Developer Option Settings

1. Unlock the main screen



2. Click on the View APPS Menu as highlighted below



3. Click on the settings in the APPS menu page (See the highlighted settings)



4. Select Developer Options


Note: If you are using Android 4.2.2 and not able to find Developer Options please check this link for more details http://zacktutorials.blogspot.ca/2013/03/enabling-developer-options-in-android.html

5. Select the following options


  1. Stay awake
  2. USB debugging
  3. Allow mock locations


Where is your USB driver for windows located

<Your Android SDK location>\extras\google\usb_driver

Ex: In my PC it is stored in the location below
C:\STRP\android-sdk-windows\extras\google\usb_driver


What to do if my extra usb_driver is not available 

In Eclipse open the Android SDK Manager  and check the Extras --> Google USB Driver.

Install the package and check in <Your Android SDK location>\extras\google\usb_driver






Windows7 USB Driver installation for nexus7

1. Device driver software was not successfully installed message will be seen as below,


2. Click Start -- Control Panel

3. Search for Device Manager


4. Check the Other devices and Nexus where it is missing driver
5.  Right Click Nexus and select Update Driver Software 


6. Click Browse my computer for driver software option



7.  Select the android driver location 

<Your Android SDK location>\extras\google\usb_driver

Ex: In my PC it is stored in the location below 
C:\Data\android-sdk-windows\extras\google\usb_driver


8. Check always trust software from "Google Inc". and Click Install


9. Your Android device is installed and message will be displayed as below,


10.  Use this option

adb kill-server
adb start-server
adb devices


Note: In case you get error 'adb' is not recognized as an internal or external command,
operable program or batch file. Please read the link http://zacktutorials.blogspot.ca/2013/04/adb-is-not-recognized-as-internal-or.html

11.  Make sure adb devices lists your device and your device is connected to USB




WindowsXP USB Driver installation for nexus7

1.  When you connect the nexus7 device the following screen will be prompted

2.  Select the option Yes, this time only and Click Next>

3.  Select the option Install from a list or specific location (Advanced)

4.  Select the Location and Click Next >


<Your Android SDK location>\extras\google\usb_driver

Ex: In my PC it is stored in the location below
C:\Data\android-sdk-windows\extras\google\usb_driver



5.  Wait for the ADB Interface driver is completely installed


6.  On Successfully Installation, you will see the screen below, 


7.  Open Command prompt and execute the adb commands as below 


adb kill-server
adb start-server
adb devices




8. If your driver is not installed. Go to Start --> My Computer --> Properties


9. Hardware --> Device Manager


10. Other --> Nexus --> Update Driver





Follow all the steps, for driver for Samsung Galaxy Nexus just download the driver below,


In case of any problem with you can download the driver directly from the link below,