Tuesday, August 31, 2010

Establishing DataBase Connection in Oracle

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package db;

import java.sql.*;

/**
 *
 * @author subinsuresh
 */
public class dbconnection {

    Statement stmt;
    Connection con;

    public void start() {
        try {
            ResultSet rs;
            //Register the JDBC driver for Oracle.
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            /*
             * Define URL of database server for database  on the localhost      
             * with the default port number 1521.      
             */
           String url = "jdbc:oracle:thin:@localhost:1521:serviceID";
            /*
             * Get a connection to the database for a user named root with the password
             * subin
             */
         con = DriverManager.getConnection(url, "root", "subin");

            //Display URL and connection information
            System.out.println("URL: " + url);
            System.out.println("Connection: " + con);
        //Get a Statement object
        stmt = con.createStatement();
        //Query the database, storing the result in an object of type ResultSet
         rs = stmt.executeQuery("SELECT * from tablename");
     } catch (Exception e) {
            e.printStackTrace();
        }//end catch
    }

    public Statement getstatement() throws SQLException {
      stmt=con.createStatement();
      return stmt;
    }
}

No comments:

Post a Comment