Archive

Archive for the ‘sql’ Category

Change default port 8080 in Oracle XE

April 22, 2011 24 comments

When two services in system running with the same port, example “8080”, you cant able to get access to those services. For this we have to change the listening port number of any services to other than “8080” port.

Here I have given how to change the listening port number of Oracle XE Database.

1. To get the current HTTP Listener Port number and FTP Port number, use the below query

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\Room>sqlplus

SQL*Plus: Release 10.2.0.1.0 - Production on Fri Apr 22 19:05:48 2011

Copyright (c) 1982, 2005, Oracle.  All rights reserved.

Enter user-name: system
Enter password:

Connected to:
Oracle Database 10g Express Edition Release 10.2.0.1.0 - Production

SQL> select dbms_xdb.gethttpport as "HTTP-Port",dbms_xdb.getftpport as "FTP-Port
" from dual;

 HTTP-Port   FTP-Port
---------- ----------
      8090          0

SQL>

2. To change the HTTP Listener Port number, use the below single command in SQL window.


SQL> begin
  2  dbms_xdb.sethttpport('80');
  3  end;
  4  /

Advertisements
Categories: sql Tags: , ,

Connecting to an Oracle database using Java

January 4, 2011 1 comment

This example uses JDBC Driver to connect to an Oracle database instance located in localhost.

sample connection string for Oracle DB

                     jdbc:oracle:thin:@localhost:1521:oracle

Find the connection string details from tnsnames.ora file located in Oracle Database installation directory

ORACLE_HOME\product\11.2.0\dbhome_1\NETWORK\ADMIN
ORACLE =
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = oracle)
    )
  )

Here the Sample code to connect with Oracle Database.

package com.room.db.connection;

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

public class DBConnectionTest {

	/**
	 * This method is to get the Database connection
	 * @return
	 */
	public static Connection getConnection(){
		Connection connection=null;
		try {
			Class.forName("oracle.jdbc.driver.OracleDriver");
			connection=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:oracle", "system", "password");
			System.out.println("--- java.sql.Connection created ----");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	/**
	 * This method is for closing the connection resources
	 * @param rs
	 * @param stmt
	 * @param conn
	 */
	public static void closeConnection(ResultSet rs,Statement stmt,Connection conn){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				rs=null;
			}
		}
		if(stmt!=null){
			try {
				stmt.close();
			} catch (SQLException e) {
				stmt=null;
			}
		}
		if(conn!=null){
			try {
				conn.close();
			} catch (SQLException e) {
				conn=null;
			}
		}
	}
	
       /**
	 * Main method to test the DB Connection and querying the DB              version
	 * @param args
	 */
	public static void main(String[] args) {
//		System.out.println(DBConnectionTest.getConnection());
		Connection conn=null;
		Statement stmt=null;
		ResultSet rs=null;
		try {
			conn=getConnection();
			stmt=conn.createStatement();
			final String query="select BANNER from SYS.V_$VERSION";
			rs=stmt.executeQuery(query);
			while(rs.next()){
				System.out.println (rs.getString(1));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			closeConnection(rs, stmt, conn);
		}
		
	}
}
Categories: Java, sql