How to Connect to an Oracle Database in Java on PC or Mac
How to Connect to an Oracle Database in Java on PC or Mac
This wikiHow teaches you how to connect to an Oracle Database in Java. In order to connect to a Java database, you first need the driver class, the URL for the oracle database, and the username and password.
Steps

Download the driver class from the Oracle website. You can download "ojdbc6.jar" or "ojdbc7.jar".

Type the following code: This code lists the process Java will go through. First it will search for the driver class, then it will connect to the Oracle database URL, then it will connect to the database. import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException;

Set the class. Type the following code to set the class. class OracleConnection{

Create a string of statements. This code will start a string of statements that will let the user know that the program is searching for driver class, and then will let the user know if the driver class is found, or that the driver class wasn't found. Type the following code: public static void main(String[] argv) {

Create Searching Statement. The following code will let the user know the program is search for the driver class. The "System.out.println();" is used to create statements. Type the following code. System.out.println("Searching for Oracle JDBC driver...");

Search for Java database driver. The following code will tell the program to search for the database driver. try { Class.forName("oracle.jdbc.driver.OracleDriver"); }

Create a return statement if database driver isn't found. If the program cannot find the database driver, the following code will generate a message that lets the user know they need the database driver. Type the following code: catch (ClassNotFoundException e) { System.out.println("Oracle JDBC driver not found!"); e.printStackTrace(); return; }

Create a return statement if database driver is found. The following code will let the user know the program has located the database driver. System.out.println("Oracle JDBC Driver Registered.");

Connect to the Oracle database. The following code will start the process of connecting to the Oracle database. Connection connection = null;

Add database URL, username, and password. The following code will enter the database URL, username and password. You must enter these values correctly in the code. A common url is "jdbc:oracle:thin:@localhost:1521:xe". The username is "system" by default, and the password is set by the user when you install the oracle database. Type the following code with the correct values: try { connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "username", "password"); }

Return error message if username and password are incorrect. Type the following code to return an error message if the URL, username, or password is incorrect. catch (SQLException e) { System.out.println("Connection Failed! Check username and password"); e.printStackTrace(); return; }

Create a database connection message. Type the following code to let the user know when the connection to the database is successful: if (connection != null) { System.out.println("Connection to database successful"); }

Create connection error message. If the program is unable to connect to the database for any reason, the following code will return a message that lets the user know there was an error. Type the following code: else { System.out.println("Unable to connect to database."); }

Close the string. Type a "}" at the last line to close the string of statements.

Close the class. Type a final "}" at the bottom of the page to close the class object. This concludes the code. Your entire code should look something like this: import java.sql.DriverManager; import java.sql.Connection; import java.sql.SQLException; class OracleConnection{ public static void main(String[] argv) { System.out.println("Searching for Oracle JDBC driver..."); try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { System.out.println("Oracle JDBC driver not found!"); e.printStackTrace(); return; } System.out.println("Oracle JDBC Driver registered."); Connection connection = null; try { connection = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "username", "password"); } catch (SQLException e) { System.out.println("Connection Failed! Check username and password"); e.printStackTrace(); return; } if (connection != null) { System.out.println("Connection to database successful"); } else { System.out.println("Unable to connect to database."); } } }

What's your reaction?

Comments

https://filka.info/assets/images/user-avatar-s.jpg

0 comment

Write the first comment for this!