Sep
16
2014

Eclipse | How To Hide Closed Project From Project / Package Explorer

This article is to about how to hide closed project from project explorer and package explorer views.

Here below is a screenshot of  workspace package explorer view where few projects are closed as not used in current development.

To close project right click on project → Close project

coffeewithcode

How to remove closed project from package / project explorer views?

Click on View Menu icon as below

coffeewithcode

Select Filters for package explorer view

coffeewithcode

Select Customize View for project explorer

coffeewithcode

By selecting above a new window will be opened containing different types of filter.

Check Closed project check box and click OK

coffeewithcode

This will remove all the closed project from the current view.

 

Your Suggestions Are Always Welcomed.

Sep
30
2012

How to install Oracle libraries for PHP5 on Ubuntu Server

An easy way to Connect Apache server (PHP) with Oracle database.

Execute following command in terminal to complete installation of oracle OCI8 extension in php.ini.

1) Get Oracle repository from this link.

     http://oss.oracle.com/debian unstable main non-free

2) Make sure to add the proper GPG key for the repository.

      sudo wget http://oss.oracle.com/el4/RPM-GPG-KEY-oracle -O- | sudo apt-key add –

3) Get all the packages for PHP5, Oracle, and Apache2 server.

       sudo apt-get update

       sudo apt-get install build-essential

       sudo apt-get install php5-dev

       sudo apt-get install php5

       sudo apt-get install php-pear

       sudo apt-get install oracle-xe-client

       sudo apt-get install apache2

       sudo apt-get install libapache2-mod-php5

4) Install Oracle OCI8 library.

       pecl install oci8

5) This will ask for your home directory. At the time of installing the Oracle client was installed in following path :

     /usr/lib/oracle/xe/app/oracle/product/10.2.0/client/

6) When the installation is complete you will need to add the extension information to both instance of php.ini.

     echo “extension=oci8.so” >> /etc/php5/cli/php.ini

     echo “extension=oci8.so” >> /etc/php5/apache2/php.ini

7) Restart Apache2 server.

    /etc/init.d/apache2 restart

 

 

Now make a simple test to find oracle connectivity with php.

Make test.php and paste following  code.

01
<?php
02
//oracle connection variables
03
$ora_user = 'USER_NAME'; //username
04
$ora_pass = 'PASSWORD'; //password
05
$ora_host = 'SERVER_IP_OF_ORACLE"'; //host name or server ip address
06
$ora_db = ‘DATABASE_NAME'; //database name
07
 
08
$conn = oci_connect($ora_user,$ora_pass,'//'.$ora_host.'/'.$ora_db);
09
 
10
// error handling
11
if (!ora_conn){ // if variable $conn fails to connect
12
// do the following if it fails
13
$ora_conn_erno = oci_error(); // insert oci_error() function into variable
14
echo ($ora_conn_erno['message']."\n"); // print the $ora_conn_erno variable/oci_error() function selecting only the message (human readable)
15
oci_close($conn); // close the connection just in case php doesn't close it
16
} else {
17
// if it doesn't fail it will proceed with the rest of the script
18
echo "Connection Successful\n"; //echo message if connection does not error
19
oci_close($conn); // close the connection
20
}
21
?>

Jul
19
2012

Php code for twitter interaction

Let’s take a look how php code interacts with twitter.

1.) How to get number of  twitter followers.

 PHP |  copy code |? 
1
fnction get_followers($twitter_id){
2
    $xml=file_get_contents('http://twitter.com/users/show.xml?screen_name='.$twitter_id);
3
    if (preg_match('/followers_count>(.*)</',$xml,$match)!=0) {
4
        $tw['count'] = $match[1];
5
    }
6
 
7
    return $tw['count'];
8
}

Now call this function with this code

 PHP |  copy code |? 
1
$followers = get_followers(‘vimalm4u’);
2
echo “You have”.$followers.”followers!;

2.) Get number of retweets for a specific page.

       Function will get the number of  retweets of the url passed as a parameter

 PHP |  copy code |? 
01
function rtweetCount($url) {
02
   $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url);
03
   $element = new SimpleXmlElement($content);
04
   $retweets = $element->story->url_count;
05
   if($retweets){
06
       return $retweets;
07
   } else {
08
       return 0;
09
   }
10
}
11
 
12
echo rtweetCount('http://www.coffeewithcode.com');

3) Shorten urls for Twitter

      As we know, we can’t tweets messages which are longer than 140 characters. To avoid this problem, you have to use an url shortener.

 

 PHP |  copy code |? 
1
function getTinyUrl($url) {
2
   return file_get_contents("http://tinyurl.com/api-create.php?url=".$url);
3
}

Jun
29
2012

How To Call A Java Script From Java Code?

This post is to describe how to call a java script from java code.

Below is  java file.In which there is a main method.In this main method a script engine instance is taken.This script engine is already with jdk.For this one need not to add any other jar file.

01
import java.io.FileNotFoundException;
02
import java.io.FileReader;
03
 
04
import javax.script.ScriptEngine;
05
import javax.script.ScriptEngineManager;
06
import javax.script.ScriptException;
07
 
08
public class Driver {
09
 
10
/**
11
* @param args
12
* @throws FileNotFoundException
13
*/
14
public static void main(String[] args) throws FileNotFoundException {
15
try {
16
/**
17
* To call a anonymous function from java script file
18
*/
19
ScriptEngine engine = new ScriptEngineManager()
20
.getEngineByName("javascript");
21
FileReader fr = new FileReader("src/js/MySpec.js");
22
engine.eval(fr);
23
 
24
} catch (ScriptException scrEx) {
25
scrEx.printStackTrace();
26
}
27
}
28
}

Below is the java script file with an anonymous fuction.This function is to print on the console.

1
// java script anonumous function
2
(function() {
3
 print('Hello world');
4
})();
5

Below is the folder structure of the project.

Folder Structure

Folder Structure

Your Suggestions Are Always Welcomed.

Jun
20
2012

Hibernate plugin in eclipse

In previous post I describe about how to hibernate plugin the eclipse.In this post I am detailing about how that plugin will be helpful in development.

While creating any new hibernate related file this plugin will help you by configuring the parameter which are necessary to create that file.

Below are the suggestion by hibernate plugin in the new wizards.

Hibernate plugin in eclipse

Hibernate plugin in eclipse

On click of any of four link it will start a new wizard.New wizard will ask for the required input parameter to build respective file.By using this wizard one need not to create specific file manually and do entries.

While working with hibernate mapping file hibernate plugin also help with suggestions of bean properties.

There may many other features of hibernate plugin which are helpful at the time of development.As I came across I will share them on this blog.

Above are the benefits at the time of the development.Hibernate plugins also provide a complete hibernate tutorial inside the eclipse with examples.

To see the hibernate tutorial inside the eclipse go to

  • Help
  • Help Contents

It will open a new windows.This contains the guide to develop hibernate application with examples.

Here below is the screenshot of newly opened window:

Hibernate plugin in eclipse

Hibernate plugin in eclipse

Your Suggestions Are Always Welcomed.

Jun
20
2012

How to install hibernate plugin in eclipse?

This article is about how to install hibernate plugin / tools in eclipse.

To install hibernate plugin / tools in eclipse one need to follow below steps.

  • Go to help menu in eclipse
  • Install new software (This will vary in different eclipse version)
Hibernate plugin installation in eclipse

Hibernate plugin installation in eclipse

  • Click on Add site
Hibernate plugin installation in eclipse

Hibernate plugin installation in eclipse

  • In location text box paste following url   

             http://download.jboss.org/jbosstools/updates/stable/

Hibernate plugin installation in eclipse

Hibernate plugin installation in eclipse

  • It will show all the jBoss software to install.If you only want to install hibernate plugin then in search text box type hibernate.It will filter out only hibernate related plugins.
Hibernate plugin installation in eclipse

Hibernate plugin installation in eclipse

  • Click next to install  (May be it will take some time to install the plugin (I f you are facing error in installing the hibernate plugin at this stage then there is a check box in above screen named contact all update site during install to find required software, please check it. It will resolve all the resolve required dependency.)
  • Once installed it will ask to restart the eclipse.
  • After restart one can verify it.
  • To verify:
  • Go to windows
  • Open perspective
  • In other option you can see the hibernate
Hibernate plugin installation in eclipse

Hibernate plugin installation in eclipse

This plugin is helpful while creating the hibernate file in new wizard.

Hibernate plugin installation in eclipse

Hibernate plugin installation in eclipse

Your Suggestions Are Always Welcomed.

Jun
18
2012

Hibernate Hello World Example With Annotation

This article is about hibernate practical example. Below is an example of hibernate with my sql using hibernate annotations.

Here is the Student bean class with id and name properties with annotations. 

01
package beans;
02
 
03
import javax.persistence.Column;
04
import javax.persistence.Entity;
05
import javax.persistence.GeneratedValue;
06
import javax.persistence.Id;
07
import javax.persistence.Table;
08
 
09
@Entity
10
@Table(name="student_master")
11
public class Student  {
12
 
13
 @Id
14
 @GeneratedValue
15
 @Column(name="stud_id")
16
 private long id;
17
 
18
 @Column(name="name")
19
 private String name;
20
 
21
 /**
22
  * getters and setters
23
  * @return
24
  */
25
 public long getId() {
26
 return id;
27
 }
28
 public void setId(long id) {
29
 this.id = id;
30
 }
31
 public String getName() {
32
 return name;
33
 }
34
 public void setName(String name) {
35
 this.name = name;
36
 }
37
}

Here is the hibernate.cfg.xml file. For this example mysql used as the data base.

01
<?xml version='1.0' encoding='utf-8'?>
02
<!DOCTYPE hibernate-configuration PUBLIC
03
"-//Hibernate/Hibernate Configuration DTD//EN"
04
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
05
 
06
<hibernate-configuration>
07
 <session-factory>
08
 <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
09
 <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
10
 <property name="hibernate.connection.username">root</property>
11
 <property name="hibernate.connection.password">root</property>
12
 <property name="show_sql">true</property>
13
 <property name="hibernate.hbm2ddl.auto">create</property>
14
 <property name="dialect">org.hibernate.dialect.MySQLDialect</property>
15
 
16
  <mapping class="beans.Student"/>
17
 </session-factory>
18
</hibernate-configuration>

Here is the Driver class. In the driver class we have created an instance of student and saved to the database.

01
package driver;
02
 
03
import org.hibernate.Session;
04
import org.hibernate.SessionFactory;
05
import org.hibernate.cfg.Configuration;
06
 
07
import beans.Student;
08
 
09
public class Driver {
10
 /**
11
  * main method
12
  */
13
 public static void main(String[] args) {
14
 Session session = null;
15
 try {
16
 //getting session factory instance
17
 SessionFactory sessionFactory = new Configuration().configure(
18
 "hibernate/hibernate.cfg.xml").buildSessionFactory();
19
 session = sessionFactory.openSession();
20
 session.beginTransaction();
21
 //student instance
22
 Student student = new Student();
23
 student.setName("Sanjay ");
24
 session.save(student);
25
 session.getTransaction().commit();
26
 } catch (Exception e) {
27
 System.out.println(e.getMessage());
28
 } finally {
29
 session.flush();
30
 session.close();
31
 }
32
 }
33
}

Your Suggestions Are Always Welcomed.

Jun
18
2012

Hibernate Hello World Example With XML Mapping

This article is about hibernate practical example. Below is an example of hibernate with my sql using hibernate mapping files.

Here is the Student bean class with id and name properties.

01
package beans;
02
 
03
public class Student  {
04
 
05
private long id;
06
private String name;
07
 
08
/**
09
* getters and setters
10
* @return
11
*/
12
public long getId() {
13
return id;
14
}
15
public void setId(long id) {
16
this.id = id;
17
}
18
public String getName() {
19
return name;
20
}
21
public void setName(String name) {
22
this.name = name;
23
}
24
}

Here is the student.hbm.xml file.

01
<?xml version="1.0"?>
02
<!DOCTYPE hibernate-mapping PUBLIC 
03
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
04
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
05
 
06
<hibernate-mapping>
07
<class name="beans.Student" table="student_master"  schema="test">
08
<id name="id" type="long" column="id">
09
<generator />
10
</id>
11
<property name="name">
12
<column name="name" />
13
</property>
14
</class>
15
</hibernate-mapping>

Here is the hibernate.cfg.xml file. For this example mysql used as the data base.

01
<?xml version='1.0' encoding='utf-8'?>
02
<!DOCTYPE hibernate-configuration PUBLIC
03
"-//Hibernate/Hibernate Configuration DTD//EN"
04
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
05
 
06
<hibernate-configuration>
07
<session-factory>
08
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
09
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test</property>
10
<property name="hibernate.connection.username">root</property>
11
<property name="hibernate.connection.password">root</property>
12
<property name="hibernate.connection.pool_size">10</property>
13
<property name="show_sql">true</property>
14
<property name="hibernate.hbm2ddl.auto">update</property>
15
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
16
<property name="hibernate.current_session_context_class">thread</property> 
17
 
18
<mapping resource="mappings/student.hbm.xml"/>
19
</session-factory>
20
</hibernate-configuration>

Here is the Driver class. In the driver class we have created an instance of student and saved to the database.

01
package driver;
02
 
03
import org.hibernate.Session;
04
import org.hibernate.SessionFactory;
05
import org.hibernate.cfg.Configuration;
06
 
07
import beans.Student;
08
 
09
public class Driver {
10
 /**
11
  * main method
12
  */
13
 public static void main(String[] args) {
14
 Session session = null;
15
 try {
16
 //getting session factory instance
17
 SessionFactory sessionFactory = new Configuration().configure(
18
 "hibernate/hibernate.cfg.xml").buildSessionFactory();
19
 session = sessionFactory.openSession();
20
 session.beginTransaction();
21
 //student instance
22
 Student student = new Student();
23
 student.setName("Sanjay ");
24
 session.save(student);
25
 session.getTransaction().commit();
26
 } catch (Exception e) {
27
 System.out.println(e.getMessage());
28
 } finally {
29
 session.flush();
30
 session.close();
31
 }
32
 }
33
}

Overall project structure.

Hibernate Project Structure

Hibernate Project Structure

Your Suggestions Are Always Welcomed.

Jun
13
2012

Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]

While working with hibernate  this error can be faced. This is because absence of ‘javaassist.jar’ in your classpath.

1
Unable to instantiate default tuplizer [org.hibernate.tuple.entity.PojoEntityTuplizer]
2
Exception in thread "main" java.lang.NullPointerException
3
at driver.Driver.main(Driver.java:39)

What is JavaAssist?

Javassist (Java programming assistant) is a load-time reflective system for Java. It is a class library for editing bytecodes in Java; it enables Java programs to define a new class at runtime and to modify a class file before the JVM loads it. Unlike other similar systems, Javassist provides source-level abstraction; programmers can modify a class file without detailed knowledge of the Java bytecode. They do not have to even write an inserted bytecode sequence; Javassist instead can compile a fragment of source text on line (for example, just a single statement). This ease of use is a unique feature of Javassit against other tools.

Click here to download and read more about javaassist.

Your Suggestion Are Always Welcomed.

Jun
12
2012

How to find hibernate version?

How to find hibernate version?

1. By printing out the it from the org.hibernate.Version class.

Like below:

1
public class Driver {
2
/**
3
* @param args
4
*/
5
public static void main(String[] args) {
6
System.out.println(org.hibernate.Version.getVersionString());
7
}
8
}

2. By the hibernate jar itself.

  • Double click on hibernate.jar 
  • Double click on META-INF
  • Double click on MANIFEST.MF

Opened file will show the information about the hibernate jar.

Here is the implementation version.

It will looks like below:

01
Manifest-Version: 1.0
02
Archiver-Version: Plexus Archiver
03
Created-By: Apache Maven
04
Built-By: gbadner
05
Build-Jdk: 1.5.0_16
06
Specification-Title: Hibernate Distribution
07
Specification-Version: 3.6.7.Final
08
Specification-Vendor: Hibernate.org
09
Implementation-Title: Hibernate Distribution
10
Implementation-Version: 3.6.7.Final
11
Implementation-Vendor-Id: org.hibernate
12
Implementation-Vendor: Hibernate.org
13
Implementation-URL: http://hibernate.org

Your Suggestions Are Always Welcomed.

Pages:1234567»