Guest User

Untitled

a guest
Jun 11th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. public static void main (String [] args) {
  2. System.out.println("-------- Oracle JDBC Connection Testing ------");
  3.  
  4. try {
  5.  
  6. Class.forName("oracle.jdbc.driver.OracleDriver");
  7.  
  8. } catch (ClassNotFoundException e) {
  9.  
  10. System.out.println("Where is your Oracle JDBC Driver?");
  11. e.printStackTrace();
  12. return;
  13.  
  14. }
  15.  
  16. System.out.println("Oracle JDBC Driver Registered!");
  17.  
  18. Connection connection = null;
  19. determineAndSetTnsHome();
  20.  
  21. try {
  22.  
  23. connection = DriverManager.getConnection(
  24. "jdbc:oracle:thin:/@xxx");
  25.  
  26. } catch (SQLException e) {
  27.  
  28. System.out.println("Connection Failed! Check output console");
  29. e.printStackTrace();
  30. return;
  31.  
  32. }
  33.  
  34. if (connection != null) {
  35. System.out.println("You made it, take control your database now!");
  36. } else {
  37. System.out.println("Failed to make connection!");
  38. }
  39. }
  40. private static void determineAndSetTnsHome() {
  41. String tnsAdmin = System.getenv("TNS_ADMIN");
  42. if (tnsAdmin == null) {
  43. String oracleHome = System.getenv("ORACLE_HOME");
  44. if (oracleHome == null) {
  45. return; //failed to find any useful env variables
  46. }
  47. tnsAdmin = oracleHome + File.separatorChar + "network" + File.separatorChar + "admin";
  48. }
  49.  
  50. System.setProperty("oracle.net.tns_admin", tnsAdmin);
  51. }
Add Comment
Please, Sign In to add comment