Friday, November 5, 2010

Call Oracle Stored Function using php

Concider the following function code :
create or replace function notify ( pname in varchar2 ) 
return varchar2 
is 
massage varchar2(100); 
begin
     massage := 'hello '||pname||' time is '||sysdate ; 
     return massage ; 
end
/


Now write php code to execute the function and catch the return value .


1.  At first connect to oracle server using the following lines


$targetAddress = "localhost/XE"; // ipAddress / servicename or sid   
$username = "hello" ;   
$password = "hello" ;   
//this line of code connects you to desired database   
$conn = oci_connect($username , $password , $targetAddress ); 


 (change the connect parameters according to yours.)


2.  Then prepare a statement using oci_parse() 


$query = oci_parse($conn , "select notify(:pname) message from dual");


3.  Lets declare a php variable which we will use as the function parameter .


$person = "Mike" ;
 
4.  after that bind the php variable with the prepared statement   $query  


using oci_bind_by_name() function 

oci_bind_by_name($query ,":pname",$person , 30 , SQLT_CHR );



5.  now execute the statement using oci_execute()


oci_execute($query);


6.  Now it's time to retrieve the value returned by the function .Like most other result retrieving process its time to fetch and find the value of a particular column  ( in this case it will be "MESSAGE" , we aliased it in the oci_parse() function )


using functions oci_fetch_array() and oci_result() we get our desired result .


while( oci_fetch_array($query))   
{        
  //retrives the value of the specified field from the row fetched by oci_fetch_array()        
  echo "
Notifier: "
 . oci_result($query,"MESSAGE");   
} 
Alternative way:


before step 5 do the following :


define a variable to get the return value by oci_define_by_name().


oci_define_by_name($query ,"MESSAGE",$message);


Then do step 5 .


after that instead of doing step 6 do the following . oci_fetch() is used instead of oci_fetch_array()


while( oci_fetch($query))  
 {        
      echo "Notifier: " . $message ;  
 }

Saturday, October 23, 2010

Jasper Report and Netbeans Integration

 In this solution I am referring  

Installation of the programs listed above is a prerequisite .

At first run NetBeans IDE and go to tools >> Libraries then click on New Library.. . enter a name you prefer and set the Library Type as Class Library. After creating this , the name  is enlisted in the Libraries list . Select the name from that list . You will find Classpath tab for the library to be empty. Click on Add JAR/Folder....

then go to the iReport installation directory
then find

.......\ireport\modules\ext  directory

here you will find the following JAR files

  • commons-beanutils-1.8.2.jar
  • commons-collections-3.2.1.jar
  • commons-digester-1.7.jar
  • commons-javaflow-20060411.jar
  • commons-logging-1.1.jar
  • groovy-all-1.5.5.jar
  • iText-2.1.7.jar
  • jasperreports-3.7.5.jar


Add the files to the classpath tab you opened. After that create a new Project and add the library you created. Now you are ready to fly with reporting.
( these are the basic needs for netbeans project to be compatible with reporting ).


Use iReport designer to create the .jrxml file and place it in a directory say C:\Reports\ .  For example let the filename is sample.jrxml and an output file be C:\Reports\output\result.pdf .

Then place the following code inside main() of the project you added the library. Then simply run and see what happens.

try
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/hello","hello","hello");
   
    JasperReport jasperReport = JasperManager.compileReport("C:/Reports/sample.jrxml");
   
    JasperPrint print = JasperManager.fillReport(jasperReport, new HashMap(), conn );    

    JasperExportManager.exportReportToPdfFile(print,"C:/Reports/output/result.pdf");
}

catch(JRException ex)
{
    System.out.println(ex.getMessage());
}

catch(SQLException ex )
{
    System.out.println(ex.getMessage());
}

catch(ClassNotFoundException ex )
{
    System.out.println(ex.getMessage());
}

File directories used in  the code ar specified as I assumed before the code segment.
Also change the connection string and DBMS according to yours.

Sunday, August 1, 2010

Connecting to oracle 10g from a remote pc using php

( all the work may be same if connecting to a database at localhost, i didn't try it )

You have to do the following-----

assuming that no php,mysql,apache server is installed in your machine . then just  download wampserver and install it .
it has all the three items I mentioned earlier .

download Instant Client Package - Basic file from here ( i used version 10.2.0.4 , windows 32-bit ) and extract it under some folder say

C:\Program Files\OracleClient ( no folder under this directory only the dll and jar files )

then add the directory mentioned ( that means the directory where you extracted the files   ) above to your PATH environment variable . you can do it here

start >> My Computer >> Properties ( clicking  right button on My Computer ) >> Advanced >> Environment Variable >> System variables 

PATH variable may already defined in your pc then just Edit it by adding a semicolon and the directory where   Instant Client Package - Basic is extracted  otherwise add it .

( while comparing ignore case , all "PATH" , "Path" , "path" means the same because environment variable is case insensitive  )

open php.ini from here

                   ..................\wamp\bin\apache\Apache2.2.11\bin\ and remove the starting semicolon ( uncomment the following line )

                   extension=php_oci8.dll ( if using oracle 10g )
                   extension=php_oci8_11g.dll ( if using oracle 11g )
but don't do the both.

( you can also edit the file by clicking wamp's task-bar icon then focusing on php  you will get a pop-up menu there you will have a  php.ini file   )



then save the file  and restart your pc .

then start wampserver If you are lucky enough then you will not have any error . That  means inclusion of oci8 module was successful.

you can verify by this..........

click here http://localhost      and you will go to wampserver home page then you will get 
phpinfo() ( you can also try to find oci8 in Loaded Extensions at wamp's homepage )

click it and try to find something like the following .







                                                                                               oci8

OCI8 Supportenabled
Version1.3.5
Revision$Revision: 1.269.2.16.2.38.2.32 $
Active Persistent Connections0
Active Connections0
Oracle Instant Client Version10.2
Temporary Lob supportenabled
Collections supportenabled







DirectiveLocal ValueMaster Value
oci8.connection_classno valueno value
oci8.default_prefetch100100
oci8.eventsOffOff
oci8.max_persistent-1-1
oci8.old_oci_close_semanticsOffOff
oci8.persistent_timeout-1-1
oci8.ping_interval6060
oci8.privileged_connectOffOff
oci8.statement_cache_size2020
                   
to check your database  connection use the following code  inside the php tag










$conn = oci_connect('username', 'password', 'ip address/service_name');

if (!$conn) 
{
   trigger_error("Could not connect to database", E_USER_ERROR);
}

else
{
   echo "Connected" ;
}










  • copy it to .... \wamp\www\yourfilename.php file 
  • change the username , password , according to yours .
  • service name - according to your Service name  
  • check the link 
                        http://localhost/yourfilename.php

if the page shows connected that means you are connected to the database.

you may need to download MSVCR71.dll and copy file to the directory where all the Instant Client Package - Basic files are situated.