Tuesday, May 24, 2011

Check process running on particular port

3 ways to check process running on particular port :
==================================

root@ubuntu:/ubuntu# lsof -w -n -i tcp:8080
COMMAND  PID   USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
tnslsnr 1401 oracle   13u  IPv4  10064      0t0  TCP *:http-alt (LISTEN)

root@ubuntu:/ubuntu# fuser -n tcp 8080
8080/tcp:             1401

root@ubuntu:/ubuntu# netstat -anp|grep :8080
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      1401/tnslsnr   
root@ubuntu:/ubuntu#

Example JDBC code to check JDBC version


Please Note : Sys user wont work will throw error connect as sysdba, so created a new user srirams and checking my jdbc connection through that.


import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.SQLException;

public class OracleJdbcTest {
    static String userid="srirams", password = "xxx";
    static String url = "jdbc:oracle:thin:@localhost:1521:XE";   

    static Connection con = null;
    public static void main(String[] args) throws Exception {
        Connection con = getOracleJDBCConnection();
        if(con!= null){
           System.out.println("Got Connection.");
           DatabaseMetaData meta = con.getMetaData();
           System.out.println("Driver Name : "+meta.getDriverName());
           System.out.println("Driver Version : "+meta.getDriverVersion());

        }else{
            System.out.println("Could not Get Connection");
        }
    }

    public static Connection getOracleJDBCConnection(){

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");   

        } catch(java.lang.ClassNotFoundException e) {
            System.err.print("ClassNotFoundException: ");
            System.err.println(e.getMessage());
        }

        try {
           con = DriverManager.getConnection(url, userid, password);
        } catch(SQLException ex) {
            System.err.println("SQLException: " + ex.getMessage());
        }

        return con;
    }

}

 ===================CODE Ends=================
Result:
=============================================


Got Connection.
Driver Name : Oracle JDBC driver
Driver Version : 10.1.0.2.0








Adding JDBC driver on Eclipse IDE for JAVA


I am using Eclipse on Ubuntu 10
Oracle 10G Xpress Edition


Link to download Jdbc drivers for Oracle Database 10g 10.1.0.2 

http://www.oracle.com/technetwork/database/enterprise-edition/jdbc101040-094982.html


Let's assume you have created an Eclipse  project called Java_project  and you want to connect to Oracle. You need to tell Eclipse where to find the jdbc driver file for Oracle. To do that you should do the following:
  • In the Java perspective right-click the project name in the Package Explorer window.
  • In the pop-up menu choose Properties
  • On the left  list of the new window choose Java Build Path
  • On the right part of the window choose the Libraries
  • Click the Add External JARs button
  • In the file chooser search and find the zip file
        /home/o/oracle/jdbc/bin/classes12.zip
  • Click Open to add it to Libraries.
Now you can close the properties window and go back to your project. You should be able to connect to Oracle.

Also check this link :

http://www.ugrad.cs.ubc.ca/~cs304/2009W2/tutorials/JDBC/OracleFromEclipse.htm


I added the below two files :

ojdbc14.jar
classes12.jar









Monday, May 23, 2011

Multi Dimension Array Test Program

========================================
package java_demo;

public class MultiDimenDemo {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                            {"Smith", "Jones"}};
        System.out.println(names[0][0] + names[1][0]); //Mr. Smith
        System.out.println(names[0][2] + names[1][1]); //Ms. Jones
    }
}


=========================================

Out Put:
Mr. Smith
Ms. Jones


How arrays pick up :

{{"Mr. ", "Mrs. ", "Ms. "}, ---> [0][0] , [0][1] , [0][2],
  {"Smith", "Jones"}};       ----> [1][0] , [1][1]



[0][0] , [0][1] , [0][2], [0][3], [0][4]
[1][0] , [1][1] , [1][2], [1][3], [1][4] 



Good Article on JAVA!
http://download.oracle.com/javase/tutorial/java/index.html






Sunday, May 22, 2011

Error running project in JDeveloper - "java.sql.SQLDataException: ORA-01882: timezone region not found"


This error is not related to system time.

Fix: heres what I did,

In  Jdeveloper, 

Will need to modify settings below two places.

1) Applications -> Default Project Properties

2) Application -> Project projecties

Run/DEBUG/PROFILE -> select Default, click edit


add  -Duser.timezone=GMT  (Under Java Options)



This Solves the Problem !!