Advertisement
Guest User

Untitled

a guest
Oct 19th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.17 KB | None | 0 0
  1.  
  2. class DBQuery
  3. {
  4.     private java.sql.Connection conn;
  5.  
  6.     DBQuery()
  7.     {
  8.         java.util.Properties info = new java.util.Properties();
  9.  
  10.         try( java.io.InputStream in =  this.getClass().getResourceAsStream("myDB.properties") )
  11.         {
  12.             if( in != null )
  13.             {
  14.                 info.load(in);
  15.                 this.conn = java.sql.DriverManager.getConnection(info.getProperty("url"), info.getProperty("user"), info.getProperty("password"));
  16.                 System.out.println("Connected to " + info.getProperty("url")+ " as User '"+info.getProperty("user")+"'!");
  17.             }
  18.         }
  19.         catch( java.io.IOException | java.sql.SQLException e1)
  20.         {
  21.             System.err.println("Could not properly connect to Database!");
  22.             System.err.println(e1);
  23.  
  24.             //safety!
  25.             try
  26.             {
  27.                 if( this.conn != null ) this.conn.close();
  28.             }
  29.             catch( java.sql.SQLException e2 )
  30.             {
  31.                 System.err.println("Closing connection failed!");
  32.                 System.err.println(e2);
  33.             }
  34.             finally { System.exit(1); }
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement