Advertisement
Guest User

Untitled

a guest
Mar 27th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.20 KB | None | 0 0
  1. import java.sql.*;
  2. class ConnTest {
  3. public static void main (String[] args) throws Exception
  4. {
  5. System.out.println ("*****************************************************************************");
  6. System.out.println ("* Testing ORACLE jdbc connectivity - by Julian Gyurov *");
  7. System.out.println ("*****************************************************************************");
  8. System.out.println ("* Loading driver ORACLE jdbc Oracle Driver *");
  9. System.out.println ("* from folder \\...\\lib\\ext\\ojdbc6.jar *");
  10. Class.forName ("oracle.jdbc.OracleDriver");
  11. System.out.println ("* *");
  12. System.out.println ("* Driver loaded OK *");
  13. System.out.println ("*****************************************************************************");
  14. System.out.println ("");
  15.  
  16. /*
  17. String machineName = "mymach.myhost.my";
  18. String port = "1521";
  19. String service = "test";
  20. String user = "sys as sysdba";
  21. String pass = "mypass";
  22. */
  23. String machineName = "";
  24. String port = "";
  25. String service = "";
  26. String user = "";
  27. String pass = "";
  28.  
  29. //Machine Name
  30. System.out.println("machineName [mymach.myhost.my]: ");
  31. java.util.Scanner scanner = new java.util.Scanner(System.in);
  32. machineName = scanner.nextLine();
  33. if( machineName.isEmpty() ){machineName="mymach.myhost.my";}
  34. //System.out.println("Your machiName: " + machineName);
  35.  
  36. //Port
  37. System.out.println("Port [1521]: ");
  38. port = scanner.nextLine();
  39. if( port.isEmpty() ){port="1521";}
  40.  
  41. //Service
  42. System.out.println("Service [test]: ");
  43. service = scanner.nextLine();
  44. if( service.isEmpty() ){service="test";}
  45.  
  46. //User
  47. System.out.println("User [sys as sysdba]: ");
  48. user = scanner.nextLine();
  49. if( user.isEmpty() ){user="sys as sysdba";}
  50.  
  51. //Pass
  52. System.out.println("Pass [mypass]: ");
  53. pass = scanner.nextLine();
  54. if( pass.isEmpty() ){pass="mypass";}
  55.  
  56. System.out.println ("");
  57. System.out.println ("machineName="+machineName);
  58. System.out.println ("");
  59. System.out.println ("port="+port);
  60. System.out.println ("");
  61. System.out.println ("service="+service);
  62. System.out.println ("");
  63. System.out.println ("user="+user);
  64. System.out.println ("");
  65. System.out.println ("pass="+pass);
  66. System.out.println ("");
  67.  
  68. //Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//mymach.myhost.my:1521/test","sys as sysdba","mypass");
  69. //("jdbc:oracle:thin:@//localhost:1521/orcl", "scott", "tiger");
  70. // @//machineName:port/SID, userid, password
  71.  
  72.  
  73. Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@//"+
  74. machineName+
  75. ":"+port+
  76. "/"+service,
  77. user,
  78. pass);
  79.  
  80. //("jdbc:oracle:thin:@//localhost:1521/orcl", "scott", "tiger");
  81. // @//machineName:port/SID, userid, password
  82. try {
  83. Statement stmt1 = conn.createStatement();
  84. Statement stmt2 = conn.createStatement();
  85. try {
  86.  
  87. ResultSet rset1 = stmt1.executeQuery("select DBID from v$database");
  88. ResultSet rset2 = stmt2.executeQuery("select BANNER from SYS.V_$VERSION");
  89.  
  90. try {
  91. System.out.println ("ORACLE VERSION:");
  92. while (rset2.next())
  93. System.out.println (rset2.getString(1)); // Print col 1
  94. System.out.println ("");
  95. }
  96. finally {
  97. try { rset2.close(); } catch (Exception ignore) {}
  98. }
  99.  
  100. try {
  101. System.out.println ("ORACLE DBID:");
  102. while (rset1.next())
  103. System.out.println (rset1.getString(1)); // Print col 1
  104. System.out.println ("");
  105. }
  106. finally {
  107. try { rset1.close(); } catch (Exception ignore) {}
  108. }
  109.  
  110. }
  111. finally {
  112. try { stmt1.close(); stmt2.close(); } catch (Exception ignore) {}
  113. }
  114. }
  115. finally {
  116. try { conn.close(); } catch (Exception ignore) {}
  117. }
  118. System.out.println ("*****************************************************************************");
  119. }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement