Advertisement
Guest User

Untitled

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