Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. public static boolean connect()
  2.     {
  3.         try
  4.         {
  5.                         Class.forName(DRIVER).newInstance();
  6.                     con=DriverManager.getConnection(URL, LOGIN, PASSWORD);
  7.  
  8.         }
  9.         catch(ClassNotFoundException ex){
  10.             //System.out.print("Error " + ex.getMessage());
  11.             JOptionPane.showMessageDialog(null, "Error " + ex.getMessage());
  12.             return false;
  13.         }catch(IllegalAccessException ex){
  14.             //System.out.print("Error " + ex.getMessage());
  15.             JOptionPane.showMessageDialog(null, "Error " + ex.getMessage());
  16.             return false;
  17.         }catch(InstantiationException ex){
  18.             //System.out.print("Error " + ex.getMessage());
  19.             JOptionPane.showMessageDialog(null, "Error " + ex.getMessage());
  20.             return false;
  21.         }catch(SQLException ex){
  22.             //System.out.print("Error " + ex.getMessage());
  23.             JOptionPane.showMessageDialog(null, "Error " + ex.getMessage());
  24.             return false;
  25.         }
  26.         return true;
  27.     }
  28.  
  29.  
  30. //Мой вариант
  31.     /**
  32.      * Установка соединения с БД.
  33.      */
  34.     public void init(){
  35.         try {
  36. //settings.getDriverclass() - это com.mysql.jdbc.Driver
  37.             Class.forName(settings.getDriverclass());
  38.             String url = String.format("jdbc:%s://%s:%d/%s", settings.getDriver(), settings.getHost(), settings.getPort(), settings.getScheme());
  39.             LOG.log(Level.SEVERE, url);
  40.         //оно из настроек логин тащит
  41.             String login = settings.getLogin();
  42.         //пароль тащит
  43.             String password = settings.getPassword();
  44.  
  45.             con = DriverManager.getConnection(url, login, password);
  46.         } catch (SQLException ex) {
  47.             LOG.log(Level.SEVERE, "Connenction failed", ex);
  48.         } catch (ClassNotFoundException ex) {
  49.             LOG.log(Level.SEVERE, String.format("DriverClass %s not found", settings.getDriverclass()), ex);
  50.         }
  51.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement