Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



how to connect advanced java with oracle

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 04-01-2011
Member
 
Join Date: Dec 2010
Posts: 79
how to connect advanced java with oracle
  

I want to make project using advanced java and oracle. I don?t have any experience of making project till yet. But my group member have a knowledge about advanced java. But we don?t know the connectivity. I had ask all of my friend, but they don?t know the connectivity. Please tell me how to connect advanced java with oracle. I?ll be very thankful to you. Please help me.

Reply With Quote
  #2  
Old 04-01-2011
Member
 
Join Date: May 2008
Posts: 962
Re: how to connect advanced java with oracle

If you want to use oracle with jdbc you need to setpath first, these can be done by following steps:
Step:1
Setting CLASSPATH
1. Right click the My Computer icon. In that Click properties
2. You will see a window System
Properties. In that window click Advanced
3. You will see a button Environment Variables. Click that button.
4. In the popup window you will see User variables for XXXXXX (your name) and System variable. Select the New button which is below the User variables for XXXXX.
5. You will see the window New User Variable. In Variable name type CLASSPATH and in Variable value type .;E:\oracle\product\10.1.0\Db_1\jdbc\lib\ojdbc14.jar
The first entry must be a period, which denotes the current directory. The second entry must be the directory for the ojdbc14.jar or classess12.zip (jdk 1.2 or below).
If CLASSPATH is not set correctly, you will get a NoClassDefFoundError error when you run a compile class.
Be sure to use a version of the JDK that is compatible with the Oracle release you are using. If you use a new release of the JDK with an older release of Oracle?s drivers
, you may encounter ?access violation? errors when executing your programs.

Step 2
The PATH environment variable should already be set for JDK

Step 3
JDK is already provided by Oracle itself(You can even use your own JDK). If you install Oracle10g It comes automatically. It lies in
E:\oracle\product\10.1.0\Db_1\jdk(I have installed oracle in E: in your case it might be c: or d: ?.)

By applying above steps you will be able to set your path for connection.
Reply With Quote
  #3  
Old 04-01-2011
Member
 
Join Date: May 2008
Posts: 991
Re: how to connect advanced java with oracle

For connection first you have to set a classpath. After setting classpath, check the connection and write a following code. These code will make your connection with oracle:
1. /**
2. * ConnectOracle.java
3. */
4. package com.javaworkspace.connectoracle;
5.
6. import java.sql.Connection;
7. import java.sql.DriverManager;
8. import java.sql.ResultSet;
9. import java.sql.Statement;
10.
11. /**
12. * @author www.javaworkspace.com
13. *
14. */
15. public class ConnectOracle {
16. public static void main(String[] args) {
17. try {
18. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
19. Connection connection = DriverManager.getConnection(
20. "jdbc:oracle:thin:@localhost:1521:orcl", "scott", "t");
21.
22. Statement statement = connection.createStatement();
23. ResultSet resultSet = statement
24. .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
25. while (resultSet.next()) {
26. System.out.println("EMPLOYEE NAME:"
27. + resultSet.getString("EMPNAME"));
28. }
29. } catch (Exception e) {
30. e.printStackTrace();
31. }
32. }
33. }
The above code will make your advanced java connection with database.
Reply With Quote
  #4  
Old 04-01-2011
Member
 
Join Date: May 2008
Posts: 1,020
Re: how to connect advanced java with oracle

To make a connection with oracle you have to set the path first, after setting classpath
You have to include a following code to make a connection.

1. /**
2. * ConnectOracle.java
3. */
4. package com.javaworkspace.connectoracle;
5.
6. import java.sql.Connection;
7. import java.sql.DriverManager;
8. import java.sql.ResultSet;
9. import java.sql.Statement;
10.
11. /**
12. * @author www.javaworkspace.com
13. *
14. */
15. public class ConnectOracle {
16. public static void main(String[] args) {
17. try {
18. DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());
19. Connection connection = DriverManager.getConnection(
20. "jdbc:oracle:thin:@localhost:1521:orcl", "scott", "t");
21.
22. Statement statement = connection.createStatement();
23. ResultSet resultSet = statement
24. .executeQuery("SELECT EMPNAME FROM EMPLOYEEDETAILS");
25. while (resultSet.next()) {
26. System.out.println("EMPLOYEE NAME:"
27. + resultSet.getString("EMPNAME"));
28. }
29. } catch (Exception e) {
30. e.printStackTrace();
31. }
32. }
33. }
Reply With Quote
  #5  
Old 04-01-2011
Member
 
Join Date: Nov 2009
Posts: 824
Re: how to connect advanced java with oracle

There are two types of driver in oracle we will use both types of driver. First, we use the thin driver to connect to Oracle and execute an update statement. Try to understand and implement the code.
import java.io.*;
import java.sql.*;

public class OraThin {
public static void main(String[] args) {
try {
Connection con=null;
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection(
"jdbc:oracle:thin:@machine_name:1521:database_name",
"scott",
"tiger");
Statement s=con.createStatement();
s.execute("
INSERT INTO BOOKS VALUES
(
'A Tale of Two Cities',
'William Shakespeare',
4567891231,
'5-JAN-1962'
)
");
s.close();
con.close();
} catch(Exception e){e.printStackTrace();}
}
}
? The package java.sql containing the JDBC classes is imported.
? The class OracleDriver, in the package oracle.jdbc.driver is dynamically loaded into the Java runtime using Class.forName(...).
? A connection is requested to the database corresponding to the connection URL with the statement DriverManager.getConnection(...).
? scott/tiger is an Oracle user/password.
? We use instances of the Connection and Statement classes to perform a database operation.
? The resources hold by these are released using their close() method.
The only element now required is the service name of the database: database_name. The remainder is resolved using the local Oracle configuration file.
DriverManager.getConnection(
"jdbc:oracle:oci8:@database_name", "scott","tiger");

By running above code you can make the conection to the database.
Reply With Quote
  #6  
Old 05-01-2011
Member
 
Join Date: Nov 2009
Posts: 754
Re: how to connect advanced java with oracle

To make a connection with a oracle you need to create a jdbc connection. you must first import the
java.sql package.
Loading a database driver:
try {
Class.forName(?sun.jdbc.odbc.JdbcOdbcDriver?); //Or any other driver
}
catch(Exception x){
System.out.println( ?Unable to load the driver class!? );
}

Step 1). Creating a oracle jdbc Connection:
DBC URL Syntax:: jdbc: <subprotocol>: <subname>

DBC URL Example:: jdbc: <subprotocol>: <subname>?Each driver has its own subprotocol
?Each subprotocol has its own syntax for the source. We?re using the jdbc odbc subprotocol, so the DriverManager knows to use the sun.jdbc.odbc.JdbcOdbcDriver.
try{
Connection dbConnection=DriverManager.getConnection(url,?loginName?,?Password?)
}
catch( SQLException x ){
System.out.println( ?Couldn?t get connection!? );
}

Step 2). Creating a jdbc Statement object:
Once a connection is obtained we can interact with the database. Connection interface defines methods for interacting with the database via the established connection. To execute SQL statements, you need to instantiate a Statement object from your connection object by using the createStatement() method.
Statement statement = dbConnection.createStatement();

Step 3). Executing a SQL statement with the Statement object, and returning a jdbc resultSet.
Test JDBC Driver Installation
import javax.swing.JOptionPane;

public class TestJDBCDriverInstallation_Oracle {

public static void main(String[] args) {
StringBuffer output = new StringBuffer();
output.append(?Testing oracle driver installation \n?);
try {
String className = ?sun.jdbc.odbc.JdbcOdbcDriver?;
Class driverObject = Class.forName(className);
output.append(?Driver : ?+driverObject+?\n?);
output.append(?Driver Installation Successful?);
JOptionPane.showMessageDialog(null, output);
} catch (Exception e) {
output = new StringBuffer();
output.append(?Driver Installation FAILED\n?);
JOptionPane.showMessageDialog(null, output);
System.out.println(?Failed: Driver Error: ? + e.getMessage());
}
}
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "how to connect advanced java with oracle"
Thread Thread Starter Forum Replies Last Post
How to connect advanced java with sql server 2000 Mewad Software Development 4 04-01-2011 04:24 PM
Advanced Cache Objects in Java StudyBoy Tips & Tweaks 3 31-07-2010 03:41 PM
Java in oracle 9i database Gerri Software Development 4 03-02-2010 08:35 PM
Comparison between core java and advanced java Prashobh Mallu Software Development 5 19-01-2010 10:57 AM


All times are GMT +5.5. The time now is 10:45 PM.