Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Prywatny konstruktor domyślny - obiekt klasy można otrzymać tylko przez wywołania funkcji getInstance().
- * Wywołuje metodę tworzącą tunel SSH umożliwiający połączenie ze zdalną bazą danych. Tunel zostaje utworzony na pierwszym dostępnym porcie w zakresie 1101 do 1200.
- */
- private DataBaseConnection() {
- int i;
- tunnel = false;
- for(i = 1101; i<=1200; ++i) {
- tunnel = doSshTunnel(i, 5432, "TWOJ_LOGIN_DO_TAURUSA", "TWOJE_HASLO_DO_TAURUSA");
- if(tunnel) {
- url = String.format("jdbc:postgresql://localhost:%d/NAZWA_BAZY_DANYCH_NA_PASCALU?allowMultiQueries=true", localPort);
- break;
- }
- }
- if(i==1201 && !tunnel)
- JOptionPane.showMessageDialog(null, "Nie udało się stworzyć tunelu SSH.");
- }
- /**
- * Metoda tworząca tunel SSH z serwerem pascal.
- */
- private boolean doSshTunnel(int localPort, int remotePort, String user, String password)
- {
- try {
- JSch jsch = new JSch();
- Session session = jsch.getSession(user, "taurus.fis.agh.edu.pl", 22);
- session.setPassword(password);
- Properties config = new Properties();
- config.put("StrictHostKeyChecking", "no");
- session.setConfig( config );
- session.connect();
- this.localPort = session.setPortForwardingL(localPort, "pascal.fis.agh.edu.pl", remotePort);
- return true;
- } catch( JSchException ex) {
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement