How to connect with Oracle DB using Java ?

Category:

Here is the code for connecting with Oracle DB using java .

please download the odbc14.jar file from the following link(http://www.java2s.com/Code/Jar/o/Downloadojdbc14jar.htm) and add it to the class path or add it to Eclipse as external jar .

If you use Eclipse like me , follow the below given step.
Go to Project->Right Click ->Properties->Java Build-> library tab-> external lib-> select obdc14.jar and this code is going to work .



import java.sql.*;
public class MainDriver {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

Connection connection = null;
try {
   // Load the JDBC driver
   String driverName = "oracle.jdbc.driver.OracleDriver";
   Class.forName(driverName);

   // Create a connection to the database
   String serverName = "10.24.148.240";
   String portNumber = "1777";
   String sid = "DOCDEV";
   String url = "jdbc:oracle:thin:@" + serverName + ":" + portNumber + ":" + sid;
   String username = "SAECMS";
   String password = "Amtrak_2011De";
   connection = DriverManager.getConnection(url, username, password);
   if(connection!= null)
    System.out.println("Connected Succussfully");
   System.out.println("Byue bye");
 
  // System.in.read();
} catch (ClassNotFoundException e) {
   // Could not find the database driver
System.out.println(""+e.getMessage());
} catch (SQLException e) {
   // Could not connect to the database
System.out.println(e.getMessage());
}

}

}


Happy Coding.



Comments (0)

Post a Comment