Opening a Connection to a Database (Cont.)


The following example shows how to open a connection to our Oracle database by using OracleDataSource.


Password (args[0])=

           

// Import the following packages to use JDBC.
import  java.sql.*;
import  java.io.*;
import  oracle.sql.*;
import  oracle.jdbc.*;
import  oracle.jdbc.pool.OracleDataSource;

class  SignIn {
  public static void  main( String args[ ] ) throws SQLException {
    String user     = "C##user_id";
    String password = args[0].trim( );
    String database = "20.185.147.112:1521/xe";

    // Open an OracleDataSource and get a connection.
    OracleDataSource ods = new OracleDataSource( );
    ods.setURL     ( "jdbc:oracle:thin:@" + database );
    ods.setUser    ( user );
    ods.setPassword( password );

    try {
      System.out.println( "<html><body>" );
      System.out.println( "<font color='#3366CC' face='Helvetica'><h3>" );
      Connection  conn = ods.getConnection( );
      if ( conn != null ) {
        System.out.println( "Successfully connected!" );
        conn.close( );
      }
    }
    catch( SQLException e ) {
      System.out.println( e );
    }
    System.out.println( "</h3></font></body</html>" );
  }
}




      Variety is the spice of life.