Advertisement
Guest User

Untitled

a guest
Sep 15th, 2017
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6.  
  7. public class ora_DBTest_Demo {
  8.  
  9. public ora_DBTest_Demo (){
  10.  
  11. }
  12.  
  13. public void testconnection_mysql (int hr_offset) {
  14. Connection connect = null;
  15. PreparedStatement preparedStatement = null;
  16. ResultSet rs;
  17.  
  18.  
  19. try {
  20. // This will load the MySQL driver, each DB has its own driver
  21. Class.forName("com.mysql.jdbc.Driver");
  22. // Setup the connection with the DB
  23. connect = DriverManager
  24. //.getConnection("jdbc:mysql://149.4.223.xxx/studentdb?"+ "user=whatever&password=whatever");
  25. .getConnection("jdbc:mysql://100.33.96.6:3306/foodatabase?" + "user=Moe&password=moemoe");
  26. //127.0.0.1 100.33.96.6:3306 25.30.88.11
  27. String qry1a = "SELECT CURDATE() + " + hr_offset;
  28.  
  29. //100.33.96.6
  30.  
  31. System.out.println(qry1a);
  32. preparedStatement = connect.prepareStatement(qry1a);
  33. // "id, uid, create_time, token for id_management.id_logtime";
  34. // Parameters start with 1
  35.  
  36. ResultSet r1=preparedStatement.executeQuery();
  37.  
  38. if (r1.next())
  39. {
  40. String nt = r1.getString(1);
  41. System.out.println(hr_offset + " hour(s) ahead of the system clock of mysql at 149.4.223.xxx is:" + nt);
  42. }
  43. r1.close();
  44. preparedStatement.close();
  45.  
  46. } catch (Exception e) {
  47. try {
  48. throw e;
  49. } catch (Exception e1) {
  50. // TODO Auto-generated catch block
  51. e1.printStackTrace();
  52. }
  53. } finally {
  54. if (preparedStatement != null) {
  55. try {
  56. preparedStatement.close();
  57. } catch (SQLException e) {
  58. // TODO Auto-generated catch block
  59. e.printStackTrace();
  60. }
  61. }
  62.  
  63. if (connect != null) {
  64. try {
  65. connect.close();
  66. } catch (SQLException e) {
  67. // TODO Auto-generated catch block
  68. e.printStackTrace();
  69. }
  70. }
  71. }
  72. }
  73.  
  74. public int testConnection (int hr_offset) {
  75. String jdbcDriver = "oracle.jdbc.driver.OracleDriver";
  76. String dbURL1 = "jdbc:oracle:thin:@bonnet19.cs.qc.edu:1521:dminor";
  77.  
  78. String userName1 = "lackner";
  79. String userPassword1 = "guest";
  80.  
  81. String oracle_driver = "oracle.jdbc.driver.OracleDriver";
  82.  
  83. Connection conn;
  84. PreparedStatement pstmt;
  85. ResultSet rs;
  86.  
  87. int flag = 0;
  88. String newTime;
  89.  
  90.  
  91. try
  92. {
  93. Class.forName(oracle_driver);
  94. }
  95. catch (Exception e)
  96. {
  97. System.out.println(e.getMessage());
  98. }
  99.  
  100. try
  101. {
  102. conn = DriverManager.getConnection(dbURL1, userName1, userPassword1);
  103. String stmtQuery = "select sysdate + " + hr_offset + " from dual";
  104. pstmt = conn.prepareStatement(stmtQuery);
  105. // pstmt.setString(1,usrname);
  106. rs = pstmt.executeQuery();
  107. if (rs.next())
  108. {
  109. newTime = rs.getString(1);
  110. System.out.println(hr_offset + " hour(s) ahead of the system clock of Oracle at bonnet19 is:" + newTime);
  111. }
  112. rs.close();
  113. pstmt.close();
  114.  
  115. try
  116. {
  117. conn.close();
  118. }
  119. catch (SQLException e) {};
  120.  
  121. }
  122. catch (SQLException e)
  123. {
  124. System.out.println(e.getMessage());
  125. flag = -1;
  126. }
  127.  
  128. return flag;
  129. }
  130.  
  131. public static void main(String[] args)
  132. {
  133. try
  134. {
  135. if (args.length != 1) {
  136. System.out.println("Usage: java -jar Ora_DBTest.jar <number_of_hr_offset>");
  137. System.out.println("Success returns errorlevel 0. Error return greater than zero.");
  138. System.exit(1);
  139. }
  140.  
  141. /* Print a copyright. */
  142. System.out.println("Example for Oracle DB connection via Java");
  143. System.out.println("Copyright: Bon Sy");
  144. System.out.println("Free to use this at your own risk!");
  145.  
  146. ora_DBTest_Demo DBConnect_instance = new ora_DBTest_Demo();
  147.  
  148.  
  149. if (DBConnect_instance.testConnection(Integer.parseInt(args[0])) == 0) {
  150. System.out.println("Successful Completion");
  151. } else {
  152. System.out.println("Oracle DB connection fail");
  153. }
  154.  
  155.  
  156. // DBConnect_instance.testconnection_mysql(Integer.parseInt(args[0]));
  157.  
  158. }
  159. catch (Exception e){
  160. // probably error in input
  161. System.out.println("Hmmm... Looks like input error ....");
  162. }
  163. }
  164.  
  165.  
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement