home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 July / Chip_1998-07_cd.bin / zkuste / JBuilder / BDK / Win / bdk_sep97.exe / _SETUP.1 / Util.java < prev    next >
Encoding:
Java Source  |  1997-09-10  |  1.7 KB  |  74 lines

  1.  
  2. package sunw.demo.select;
  3.  
  4. /**
  5.  * Internal utility class for database viewer bean.
  6.  */
  7.  
  8. import java.sql.*;
  9.  
  10. class Util {
  11.  
  12.     static Connection connect(String url, 
  13.         String user, String password,
  14.         java.awt.Label status) {
  15.         status.setText("Connecting to " + url);
  16.     try {
  17.         // Make sure there is a JDBC-ODBC driver loaded.
  18.         Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
  19.     } catch (Exception ex) {
  20.         status.setText("Couldn't load JDBC-ODBC bridge driver");
  21.         System.err.println("Caught Exception : " + ex);
  22.         ex.printStackTrace();
  23.         return null;
  24.     }
  25.  
  26.         java.util.Properties info = new java.util.Properties();
  27.         info.put("user", user);
  28.         info.put("password", password);
  29.  
  30.     // We make two attempts.  If the first attempt fails
  31.     // we pause for a few seconds before the second attempt.
  32.     // This works around some weird problems on wombat.
  33.  
  34.     try {
  35.         Connection con = DriverManager.getConnection(url, info);
  36.         // Success!
  37.         return con;
  38.     } catch (Exception ex) {
  39.  
  40.         // Drop through and retry.
  41.     }
  42.  
  43.         status.setText("Connecting to " + url + " (retry)");
  44.     try {
  45.         Thread.sleep(1000);
  46.  
  47.         Connection con = DriverManager.getConnection(url, info);
  48.     
  49.         // Success!
  50.         return con;
  51.     } catch (SQLException sx)  {
  52.         status.setText("Caught " + sx);
  53.         System.err.println("Caught SQLException : " + sx);
  54.         sx.printStackTrace();
  55.     } catch (Exception ex) {
  56.         status.setText("Couldn't load JDBC-ODBC bridge driver");
  57.         System.err.println("Caught Exception : " + ex);
  58.         ex.printStackTrace();
  59.     }
  60.  
  61.     return null;
  62.     }
  63.  
  64.     static void disconnect(Connection con) {
  65.     if (con != null) {
  66.         try {
  67.         con.close();
  68.         } catch (SQLException sx)  {
  69.         System.err.println("trouble closing connection: " + sx);
  70.         }
  71.     }
  72.     }
  73. }
  74.