Advertisement
Guest User

Untitled

a guest
Nov 13th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. package ua.inf.smart.temporary.data_base.horst.monitor;
  2.  
  3.  
  4. import java.sql.*;
  5.  
  6. public class Monitor {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. try (Connection sqlBase = getSQLConnection();
  11. Connection derbyBase = getDerbyConnection()) {
  12.  
  13. toConsole(sqlBase, "SELECT * FROM donations");
  14.  
  15. toConsole(derbyBase, "SELECT * FROM Phone");
  16.  
  17. } catch (SQLException e) {
  18. e.printStackTrace();
  19. }
  20.  
  21.  
  22. }
  23.  
  24. private static void toConsole(Connection con, String statement) {
  25. try (Statement stmt = con.createStatement()) {
  26. ResultSet resultSet = stmt.executeQuery(statement);
  27. display(resultSet);
  28. } catch (SQLException e) {
  29. e.printStackTrace();
  30. }
  31. }
  32.  
  33. private static void display(ResultSet resultSet) throws SQLException {
  34. ResultSetMetaData metaData = resultSet.getMetaData();
  35. int columnCount = metaData.getColumnCount();
  36. for (int i = 1; i <= columnCount; i ++)
  37. {
  38. if (i > 1) System.out.print(", ");
  39. System.out.print(metaData.getColumnLabel(i));
  40. }
  41. System.out.println();
  42. while (resultSet.next())
  43. {
  44. for (int i = 1; i <= columnCount; i ++)
  45. {
  46. if (i > 1) System.out.print(", ");
  47. System.out.print(resultSet.getString(i));
  48. }
  49. System.out.println();
  50. }
  51. resultSet.close();
  52. }
  53.  
  54. private static Connection getSQLConnection() {
  55. Connection conn = null;
  56. try {
  57. Class.forName("com.mysql.jdbc.Driver").newInstance();
  58. conn = DriverManager.getConnection(
  59. "jdbc:mysql://localhost:3306/mydbtest",
  60. "root",
  61. "1234");
  62. } catch (SQLException e) {
  63. e.printStackTrace();
  64. } catch (IllegalAccessException e) {
  65. e.printStackTrace();
  66. } catch (InstantiationException e) {
  67. e.printStackTrace();
  68. } catch (ClassNotFoundException e) {
  69. e.printStackTrace();
  70. }
  71. return conn;
  72. }
  73.  
  74. private static Connection getDerbyConnection() {
  75. Connection conn = null;
  76. try {
  77. Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
  78. conn = DriverManager.getConnection(
  79. "jdbc:derby://localhost:1527/TestBase;create=true;user=admin;password=secret",
  80. "admin",
  81. "secret");
  82. } catch (SQLException e) {
  83. e.printStackTrace();
  84. } catch (IllegalAccessException e) {
  85. e.printStackTrace();
  86. } catch (InstantiationException e) {
  87. e.printStackTrace();
  88. } catch (ClassNotFoundException e) {
  89. e.printStackTrace();
  90. }
  91. return conn;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement