Posts

Showing posts from 2011

Manually install a maven dependency

execute the following command from the command line with the correct values for options. -Dfile option locates the downloaded jar file. Therefore make sure to specify the correct location or reach the jar location from the command line and execute the command.


mvn install:install-file -DgroupId=ojdbc -DartifactId=ojdbc6 \ -Dversion=10.2.0.3.0 -Dpackaging=jar -Dfile=ojdbc6.jar -DgeneratePom=true

How to add Maven dependency from a relative path referencing a local jar

When you want to add a dependency which is not stored in maven repositories to your project this is the way!! You can manually download it and include inside the project and refer it from the pom.xml through a relative path.

This example assumes that you have downloaded and stored the jar inside ${project}/src/test/resources/lib.  ojdbcojdbc6system6${basedir}/src/test/resources/lib/ojdbc6.jar Here we must specify the scope of the dependency  as 'system'. There is another way which is to manually install the maven dependency to your local repository. You can find out how from here.

Oracle Stored Procedure to return multiple records and call it in SQLPLUS

To return multiple records we need a REF CURSOR type variable to assign each record. The following is a script which return a REF CURSOR  as OUT parameter.

--OracleStroredProc.sql

CREATE OR REPLACE PACKAGE Types AS   TYPE cursor_type IS REF CURSOR; END Types; /
-------
CREATE OR REPLACE PROCEDURE getEmployeeInfo(p_recordset1 OUT  Types.cursor_type) AS
BEGIN     OPEN p_recordset1 FOR     SELECT empNumber, empName, phone, address FROM Employees; END;
/
We can run the above script in SQLPLUS as below.

SQL> START OracleStroredProc.sql
So procedure will be created by now. To call the procedure we have to create a variable of type REFCURSOR and pass it as a parameter to procedure call. Then we can
print the variable to see the result.


SQL> var a REFCURSOR; SQL> call getEmployeeInfo(:a);
Call completed.
SQL> print :a;

Executing the Oracle PL/SQL returns a number?

When you execute a PL/SQL script through sqlplus, if it returns a number at each line you press enter for a new line, it is beacuse you have forgot
to include '/' end of the script file for execute.

ex:

SQL> OracleScript.sql        > 34        > 35
So at this situation just enter '/'. Script will execute.

SQL> OracleScript.sql        > 34 /

If you don't want to manually enter '/' here, place a '/' end of the script file. The script in here has a '/' end of the file which tells to execute it.

Oracle change the default date format before insert data

Oracle accepts date in the default format of YYYY-MON-DD. Here month is accepted as three characters of month name. Ex: January-JAN, February: FEB, etc

Now before executing a bulk records to a table with a required date format such as YYYY-MM-DD, we can alter the session to accept date in that format from the following query.

SQL> alter session set NLS_DATE_FORMAT='YYYY-MM-DD';
Now we can insert records with date represented in YYYY-MM-DD format.

Oracle drop a schema with all the objects (Similar to database drop in MySQL)

Here we have to drop the user who is the owner of the schema objects with CASCADE option which will result in droping user with the all objects created by that user.

SQL> Drop user username CASCADE;

Oracle PL/SQL script to drop a user if not exists

This script will run a query to count the number of users with a specified user name and if the count!=0 then drop user query is executed.


DECLARE     u_count number;     user_name VARCHAR2 (50);
    BEGIN
        u_count :=0;         user_name :='datauser';         SELECT COUNT (1) INTO u_count FROM dba_users WHERE username = UPPER (user_name);
             IF u_count != 0              THEN                  EXECUTE IMMEDIATE ('DROP USER '||user_name||' CASCADE');               END IF;
              u_count := 0;         EXCEPTION            WHEN OTHERS               THEN                      DBMS_OUTPUT.put_line (SQLERRM);                      DBMS_OUTPUT.put_line ('   ');
    END;
/


Include the above script in a file .sql (Ex: DropOracleUser.sql). Make sure to replace the username with correct name which is 'datauser' in above script. You can run the above oracle PL/SQL script in sqlPlus as follows.

sql> START DropOracleUser.sql

PHP-SOAP web service with out a WSDL

This article is going to describe how to create a simple web service with php-soap step by step.

Requirements

    Server to run php (apache, xampp , wamp)
PHP-SOAP extension

Steps

Let's write a simple web service to find the length of a given string.

So here is the code for StringLengthServer.php

<?php
# StringLengthServer.php
# Copyright (c) 2011 by Lakmali Baminiwatta
#

    function findLength($string) {
           return "Length of the string " . $string . "is : ".strlen($string);
    }

    $server = new SoapServer(null,
              array('uri' => "urn://www.lakmali.lk"));

    $server->addFunction("findLength");
       $server->handle(); 
?>
First thing done here is to write a function to perform the action done by the service. So here function findLength($string) takes a string as the argument and prints back the length of it.Then SoapServer is created. First argument null which means no WSDL used. Second argumen…

Java Initialization order

When a java class get loaded first all the static fields will be loaded with their default values. Then static inits of the class begins in order, which means static fields will be initialized as given in order.

So in the given example code line 1 will load the class LakmaliClass with all its static fields assigning to default values.Then static inits will begin executing line 5, 6 and 7.Then instance inits will begin executing line 2, 3 and 4.Line 2 will load the class Second with all the static fields default values (Here no static fields)Then object will be created with passed values.Line 3 will also do the same.

class Initializer{

    public static void main(String args[]){
        LakmaliClass o=new LakmaliClass();// line 1

    }

}

class LakmaliClass{


    {
        System.out.println("Lakmali class instance init"); //line 2
        Second s1=new Second(s);            //line 3
        Second s2=new Second(number1);            //line4
    }

    static String text1="text…

Spring at a glance

Spring
Spring is an open source framework created to address the complexity of enterprise application development.
Advantages of Spring framework
Spring is a layered architecture. We can use what is required from those and leave others.
Spring enables POJO programming. POJO facilitates continuous integration, easier upgrades and switching.
The IOC feature of Spring simplifies JDBC
Open source framework.

POJO -Plain Old Java Object

Maintaining an enterprise java application for few years is even difficult to imagine due to their tightly coupled infrastructure frameworks which evolves rapidly.
Solution is using POJO. There is no magic in POJOs. There the classes don't implement infrastructure framework-specific interfaces. They are just plain Java classes. Therefore POJO decouples applications from changing frameworks. As a result, upgrading or switching to a new framework and integration will be easier and less risky.

Inversion of Control (IOC)

The concept of IOC pattern is that objects are …