daily pastebin goal
70%
SHARE
TWEET

Untitled

a guest Sep 25th, 2016 100 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.sql.*;
  2.  
  3. class SQLServerDatabase {
  4.     private String uri;
  5.     private String user;
  6.     private String password;
  7.     private Connection conn;
  8.  
  9.     public SQLServerDatabase(String uri, String user, String password) {
  10.         this.uri = uri;
  11.         this.user = user;
  12.         this.password = password;
  13.     }
  14.  
  15.     public boolean connectSQL() {
  16.         String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
  17.            
  18.         // load driver //classpaths... new... add jar
  19.         try{
  20.             Class.forName( driver );
  21.         }
  22.         catch(ClassNotFoundException cnfe){
  23.             return false;
  24.         }
  25.  
  26.         // connect to the DB
  27.         try{
  28.             this.conn = DriverManager.getConnection( this.uri, this.user, this.password );
  29.             return true;
  30.         }
  31.         catch (SQLException sqle){
  32.             return false;
  33.         }
  34.     }
  35.  
  36.     public boolean disconnectSQL() {
  37.         // close the DB
  38.         try {
  39.             if (this.conn != null) {
  40.                 this.conn.close();
  41.             }
  42.         return true;
  43.         }
  44.         catch(SQLException sqle) {
  45.             return false;
  46.       }
  47.     }
  48. }
  49.  
  50. class Main {
  51.   public static void main(String[] args) {
  52.     //String uri = "jdbc:sqlserver://theodore.ist.rit.edu;databaseName=Jobs";
  53.                  
  54.     String uri = args[0];
  55.     String user = args[1];
  56.     String password = args[2];
  57.  
  58.     SQLServerDatabase database = new SQLServerDatabase(uri, user, password);
  59.     boolean isConn = database.connectSQL();
  60.  
  61.     if (isConn) {
  62.         System.out.println("Connected to SQL database");
  63.     }
  64.     else {
  65.         System.out.println("Unable to connect to SQL database");
  66.     }
  67.  
  68.     boolean isDisconn = database.disconnectSQL();
  69.  
  70.     if (isDisconn) {
  71.         System.out.println("Disconnected from SQL database");
  72.     }
  73.     else {
  74.         System.out.println("Unable to disconnect from SQL database");
  75.     }
  76.   }
  77. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top