blogger templates blogger widgets
This is part of a list of blog posts.
To browse the contents go to

JDBC connections using DataSouce (driver implementation)


Let's try to create a stand-alone application using Datasource implementation that's available within the driver.

package com.prodapps;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import oracle.jdbc.pool.OracleDataSource;

public class DataSourceExample {

 public static void main(String[] args) throws SQLException {

  Connection conn = null;
  try {
   OracleDataSource ods = new OracleDataSource();
   ods.setDriverType("thin");
   ods.setServerName("localhost");
   ods.setNetworkProtocol("tcp");
   ods.setDatabaseName("XE");
   ods.setPortNumber(1521);
   ods.setUser("SYSTEM");
   ods.setPassword("password");
   conn = ods.getConnection();
   Statement st = conn.createStatement();
   ResultSet rs = st.executeQuery("select 777 from dual");

   while (rs.next()) {
    System.out.println(rs.getString(1));
    ;
   }
   rs.close();
   st.close();
  } finally {
   if (conn != null)
    try {
     conn.close();
    } catch (Exception ignore) {
    }
  }
 }
}

A web-application version is left to the reader to try out.


No comments:

Post a Comment