Showing posts with label java jdbc. Show all posts
Showing posts with label java jdbc. Show all posts

Tuesday, May 24, 2011

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