Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main (String [] args) {
- System.out.println("-------- Oracle JDBC Connection Testing ------");
- try {
- Class.forName("oracle.jdbc.driver.OracleDriver");
- } catch (ClassNotFoundException e) {
- System.out.println("Where is your Oracle JDBC Driver?");
- e.printStackTrace();
- return;
- }
- System.out.println("Oracle JDBC Driver Registered!");
- Connection connection = null;
- determineAndSetTnsHome();
- try {
- connection = DriverManager.getConnection(
- "jdbc:oracle:thin:/@xxx");
- } catch (SQLException e) {
- System.out.println("Connection Failed! Check output console");
- e.printStackTrace();
- return;
- }
- if (connection != null) {
- System.out.println("You made it, take control your database now!");
- } else {
- System.out.println("Failed to make connection!");
- }
- }
- private static void determineAndSetTnsHome() {
- String tnsAdmin = System.getenv("TNS_ADMIN");
- if (tnsAdmin == null) {
- String oracleHome = System.getenv("ORACLE_HOME");
- if (oracleHome == null) {
- return; //failed to find any useful env variables
- }
- tnsAdmin = oracleHome + File.separatorChar + "network" + File.separatorChar + "admin";
- }
- System.setProperty("oracle.net.tns_admin", tnsAdmin);
- }
Add Comment
Please, Sign In to add comment