Friday, July 26, 2013

Java to Oracle Database Connection with net beans IDE

package Utility;

/**
 *
 * @author Sakib
 * Datacraft LTD
 */

import java.sql.DriverManager;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.*;
public class OracleJDBC {
  
    public static void main(String[] argv) {

        System.out.println("** ------ Oracle JDBC Connection Testing ------ **");

        try {

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

        } catch (ClassNotFoundException e) {

            System.out.println("Where is your Oracle JDBC Driver?");
            e.printStackTrace();
            return;

        }

        System.out.println("Oracle driver registered");
        Connection conn=null;

        try {

            conn = DriverManager.getConnection( "jdbc:oracle:thin:@127.0.0.1:1521:xe", "sakib","Abc999");

            Statement stmt= conn.createStatement();
            ResultSet r=stmt.executeQuery("Select * from employees");
            while(r.next()){
            String str= r.getString("FIRSTNAME");
            System.out.println (str);
           
            }

        } catch (SQLException e) {

            System.out.println("Connection Failed! Check output console");
            e.printStackTrace();
            return;

        }
    }
}

No comments:

Post a Comment