Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.60 KB | None | 0 0
  1. try
  2. {
  3.  
  4. // Doing some JDBC Connections here
  5.  
  6. Map<String,Connection> connections = new HashMap<>();
  7.  
  8. while(DeviceRS.next()){
  9. final String ip_address = DeviceRS.getString("Connection_vch");
  10. System.out.println("Value of the IP Address Field:"+ip_address);
  11. connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
  12. }
  13.  
  14. for(final String ip : connections.keySet())
  15. {
  16.  
  17. // Selecting and inserting into database here ...
  18.  
  19. }// ENd of for loop
  20.  
  21. }// ENd of try block
  22. catch(SQLException ex){
  23.  
  24. ex.printStackTrace();
  25.  
  26. }
  27.  
  28. // Doing some JDBC Connections here
  29.  
  30.  
  31.  
  32.  
  33.  
  34. Map<String,Connection> connections = new HashMap<>();
  35. try
  36. {
  37.  
  38. while(DeviceRS.next()){
  39. try{
  40. final String ip_address = DeviceRS.getString("Connection_vch");
  41. connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
  42. }
  43. catch(SQLException ex){
  44. ex.printStackTrace();
  45.  
  46. }
  47. }
  48. catch(Exception ex){
  49.  
  50. ex.printStackTrace();
  51. System.out.println("in catch block");
  52. //System.exit(0); // dont use this -in real time projects
  53. }finally{
  54. System.out.println("in finally block");
  55.  
  56. }
  57.  
  58. for(final String ip : connections.keySet())
  59. {
  60.  
  61. // Selecting and inserting into database here ...
  62.  
  63. }// ENd of for loop
  64.  
  65. }// ENd of try block
  66.  
  67. try {
  68. // your code here
  69.  
  70. for(final String ip : connections.keySet()) {
  71. try { // inner catch
  72. // your code, that will be continued if sql exception occurs
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. }
  76. }
  77.  
  78. } catch (SQLException e) {
  79. e.printStackTrace();
  80. }
  81.  
  82. boolean check = true;
  83.  
  84. while(check) {
  85. try {
  86.  
  87. Map<String,Connection> connections = new HashMap<>();
  88.  
  89. while(DeviceRS.next()){
  90.  
  91. final String ip_address = DeviceRS.getString("Connection_vch");
  92. connections.put(ip_address,DriverManager.getConnection("jdbc:mysql://" + ip_address + ":3306/test",RemoteUser,RemotePass));
  93.  
  94. } // Ends inner while
  95.  
  96. check = false; //As connection is ok,we don't need to loop back.
  97.  
  98. for(final String ip : connections.keySet())
  99. {
  100.  
  101. // Selecting and inserting into database here ...
  102. }
  103.  
  104. }// Ends try block
  105.  
  106. catch(SQLException ex){
  107.  
  108. ex.printStackTrace();
  109.  
  110. } //Ends catch block
  111.  
  112. finally{
  113.  
  114. // close the connection if needed.
  115. } // Ends finally block
  116.  
  117. }// Ends outer while loop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement