Home arrow static arrow Java Programming [Archive] - connect oracle:jdbc
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - connect oracle:jdbc
This topic has 6 replies on 1 page.

Posts:205
Registered: 12/1/03
connect oracle:jdbc  
Aug 4, 2004 7:04 AM



 
hello how can i connect to oracle using jdbc?

con=java.sql.DriverManager.getConnection("jdbc:oracle:thin@xxxx.es","yy","xxxxx");

how can i know the port?
 

Posts:19,725
Registered: 9/26/01
Re: connect oracle:jdbc  
Aug 4, 2004 7:17 AM (reply 1 of 6)



 
jdbc:oracle:thin:@host:port:dbName
Usually the port is 1521, but that is up to whoever set up the Oracle server.
Or what else do you mean by "how can i know the port?"?
 

Posts:3,258
Registered: 00-08-28
Re: connect oracle:jdbc  
Aug 4, 2004 12:43 PM (reply 2 of 6)



 
/**   * Creates a database connection object using DataSource object. Please   * substitute the database connection parameters with appropriate values in   * Connection.properties file   */	private Connection dbConnection()	{		try		{			System.out.println("Trying to connect to the Database");			// Load the properties file to get the connection information     			Properties prop = loadParams("Connection");			// Create a OracleDataSource instance      			OracleDataSource ods = new OracleDataSource();			// Sets the driver type     			ods.setDriverType("thin");			// Sets the database server name  			String aaa = (String) prop.get("HostName");			ods.setServerName((String) prop.get("HostName"));			// Sets the database name 			String bbb = (String) prop.get("SID");			ods.setDatabaseName((String) prop.get("SID"));			// Sets the port number 			String ccc = (String) prop.get("Port");			ods.setPortNumber(				new Integer((String) prop.get("Port")).intValue());			// Sets the user name  			String ddd = (String) prop.get("UserName");			ods.setUser((String) prop.get("UserName"));			// Sets the password  			String eee = (String) prop.get("Password");			ods.setPassword((String) prop.get("Password"));			conn = ods.getConnection();			System.out.println(				"Connected to "					+ prop.get("SID")					+ " Database as "					+ prop.get("UserName")					+ ". Please click on SELECT"					+ " button to view records");		} catch (SQLException ex)		{ // Trap SQL errors       			System.out.println(				"Error in Connecting to the Database " + '\n' + ex.toString());		} catch (IOException ex)		{			// Trap IO errors        			System.out.println(				"Error in reading the properties file " + '\n' + ex.toString());		}		return conn;	}


And in your code where you want to make the connection you could

if (conn == null)				conn = dbConnection();


Also you would need a Connection.properties filw which might look like

# Your Database Connection details HostName   = localhostSID        = DbPort       = 1521UserName   = SystemPassword   = system
 

Posts:3,258
Registered: 00-08-28
Re: connect oracle:jdbc  
Aug 4, 2004 12:46 PM (reply 3 of 6)



 
Sorry missed the code for loading properties

/**   * This method reads Connection.properties file which is passed as   	 * the parameter to it and load it into a java Properties	 * object and returns it.   	 **/	private Properties loadParams(String file) throws IOException	{		// Loads a ResourceBundle and creates Properties from it   		Properties prop = new Properties();		ResourceBundle bundle = ResourceBundle.getBundle(file);		Enumeration enum = bundle.getKeys();		String key = null;		while (enum.hasMoreElements())		{			key = (String) enum.nextElement();			prop.put(key, bundle.getObject(key));		}		return prop;	}
 

Posts:205
Registered: 12/1/03
Re: connect oracle:jdbc  
Aug 4, 2004 12:48 PM (reply 4 of 6)



 
thanks .

also, i would like to know how can i do the connection using jndi. the class that need the connection is a java class(pojo) and i cannot access the context. is that possible? I�m using sun one application server but i imagine that the code will be similar.
 

Posts:3,258
Registered: 00-08-28
Re: connect oracle:jdbc  
Aug 4, 2004 12:56 PM (reply 5 of 6)



 
thanks .

also, i would like to know how can i do the connection
using jndi. the class that need the connection is a
java class(pojo) and i cannot access the context. is
that possible? I�m using sun one application server
but i imagine that the code will be similar.


no problem, but I would suggest you trying to write some code first and then ask and I am sure many including me would be glad to help.
 

Posts:205
Registered: 12/1/03
Re: connect oracle:jdbc  
Aug 5, 2004 8:36 AM (reply 6 of 6)



 
i know all about the properties the only think that i didn�t know what the url for databse connection.
 
This topic has 6 replies on 1 page.