private Connection getMySQLDBConnection() throws SQLException { try { Class.forName("com.mysql.jdbc.Driver"); } catch (ClassNotFoundException e) { System.out.println("Where is your MySQL JDBC Driver?"); e.printStackTrace(); return null; } System.out.println("MySQL JDBC Driver Registered!"); String url = "jdbc:mysql://hostname:port/dbname"; String user = "******"; String password = "******"; Connection connection = null; try { connection = DriverManager.getConnection(url, user, password); } catch (SQLException e) { System.out.println("Connection Failed! Check output console"); e.printStackTrace(); return null; } return connection; }
private Connection getMSAccessDBConnection() throws SQLException, ClassNotFoundException { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // String url = "jdbc:odbc:StudentSample"; //String from ODBC manager String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ=" + "src/main/resources/Student.mdb;"; String user = ""; String password = ""; return DriverManager.getConnection(url, user, password); }
private Connection getMySQLSSHConnection() { Connection connection = null; // int assigned_port; final int local_port = 3309; // Remote host and port final int remote_port = 3306; final String remote_host = "remote.host.com"; try { JSch jsch = new JSch(); // Create SSH session. Port 22 is your SSH port which // is open in your firewall setup. System.out.println("DEBUG: get session"); Session session = jsch.getSession("user", remote_host, 22); System.out.println("DEBUG: set password"); session.setPassword("password"); // Additional SSH options. See your ssh_config manual for // more options. Set options according to your requirements. java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); config.put("Compression", "yes"); config.put("ConnectionAttempts", "2"); System.out.println("DEBUG: set configuration"); session.setConfig(config); // Connect System.out.println("DEBUG: SSH connect"); session.connect(); // Create the tunnel through port forwarding. // This is basically instructing jsch session to send // data received from local_port in the local machine to // remote_port of the remote_host // assigned_port is the port assigned by jsch for use, // it may not always be the same as // local_port. System.out.println("DEBUG: get assigned port"); assigned_port = session.setPortForwardingL(local_port, remote_host, remote_port); } catch (JSchException e) { System.out.println("DEBUG: SSH exception: fail"); e.printStackTrace(); return null; } if (assigned_port == 0) { System.out.println("Port forwarding failed !"); return null; } // Database access credintials. Make sure this user has // "connect" access to this database; // these may be initialized somewhere else in your code. final String database_user = "******"; final String database_password = "******"; final String database = "db_name"; // Build the database connection URL. StringBuilder url = new StringBuilder("jdbc:mysql://localhost:"); // use assigned_port to establish database connection url.append(assigned_port) .append("/") .append(database) .append("?user="******"&password="******"DEBUG: load mysql driver"); Class.forName("com.mysql.jdbc.Driver").newInstance(); System.out.println("DEBUG: get connection"); connection = DriverManager.getConnection(url.toString()); } catch (Exception e) { System.out.println("DEBUG get connection failed"); e.printStackTrace(); } return connection; }