Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ua.inf.smart.temporary.data_base.horst.monitor;
- import java.sql.*;
- public class Monitor {
- public static void main(String[] args) {
- try (Connection sqlBase = getSQLConnection();
- Connection derbyBase = getDerbyConnection()) {
- toConsole(sqlBase, "SELECT * FROM donations");
- toConsole(derbyBase, "SELECT * FROM Phone");
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static void toConsole(Connection con, String statement) {
- try (Statement stmt = con.createStatement()) {
- ResultSet resultSet = stmt.executeQuery(statement);
- display(resultSet);
- } catch (SQLException e) {
- e.printStackTrace();
- }
- }
- private static void display(ResultSet resultSet) throws SQLException {
- ResultSetMetaData metaData = resultSet.getMetaData();
- int columnCount = metaData.getColumnCount();
- for (int i = 1; i <= columnCount; i ++)
- {
- if (i > 1) System.out.print(", ");
- System.out.print(metaData.getColumnLabel(i));
- }
- System.out.println();
- while (resultSet.next())
- {
- for (int i = 1; i <= columnCount; i ++)
- {
- if (i > 1) System.out.print(", ");
- System.out.print(resultSet.getString(i));
- }
- System.out.println();
- }
- resultSet.close();
- }
- private static Connection getSQLConnection() {
- Connection conn = null;
- try {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- conn = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/mydbtest",
- "root",
- "1234");
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return conn;
- }
- private static Connection getDerbyConnection() {
- Connection conn = null;
- try {
- Class.forName("org.apache.derby.jdbc.ClientDriver").newInstance();
- conn = DriverManager.getConnection(
- "jdbc:derby://localhost:1527/TestBase;create=true;user=admin;password=secret",
- "admin",
- "secret");
- } catch (SQLException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- return conn;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement