Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. import java.*;
  2. public class Connect{
  3.      private java.sql.Connection  con = null;
  4.      private final String url = "jdbc:microsoft:sqlserver://";
  5.      private final String serverName= "localhost";
  6.      private final String portNumber = "1433";
  7.      private final String databaseName= "liquibase";
  8.      private final String userName = "dbuser";
  9.      private final String password = "dbuser";
  10.      // Informs the driver to use server a side-cursor,
  11.      // which permits more than one active statement
  12.      // on a connection.
  13.      private final String selectMethod = "cursor";
  14.      
  15.      // Constructor
  16.      public Connect(){}
  17.      
  18.      private String getConnectionUrl(){
  19.           return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
  20.      }
  21.      
  22.      private java.sql.Connection getConnection(){
  23.           try{
  24.                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");
  25.                con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
  26.                if(con!=null) System.out.println("Connection Successful!");
  27.           }catch(Exception e){
  28.                e.printStackTrace();
  29.                System.out.println("Error Trace in getConnection() : " + e.getMessage());
  30.          }
  31.           return con;
  32.       }
  33.  
  34.      /*
  35.           Display the driver properties, database details
  36.      */
  37.  
  38.      public void displayDbProperties(){
  39.           java.sql.DatabaseMetaData dm = null;
  40.           java.sql.ResultSet rs = null;
  41.           try{
  42.                con= this.getConnection();
  43.                if(con!=null){
  44.                     dm = con.getMetaData();
  45.                     System.out.println("Driver Information");
  46.                     System.out.println("\tDriver Name: "+ dm.getDriverName());
  47.                     System.out.println("\tDriver Version: "+ dm.getDriverVersion ());
  48.                     System.out.println("\nDatabase Information ");
  49.                     System.out.println("\tDatabase Name: "+ dm.getDatabaseProductName());
  50.                     System.out.println("\tDatabase Version: "+ dm.getDatabaseProductVersion());
  51.                     System.out.println("Avalilable Catalogs ");
  52.                     rs = dm.getCatalogs();
  53.                     while(rs.next()){
  54.                          System.out.println("\tcatalog: "+ rs.getString(1));
  55.                     }
  56.                     rs.close();
  57.                     rs = null;
  58.                     closeConnection();
  59.                }else System.out.println("Error: No active Connection");
  60.           }catch(Exception e){
  61.                e.printStackTrace();
  62.           }
  63.           dm=null;
  64.      }    
  65.      
  66.      private void closeConnection(){
  67.           try{
  68.                if(con!=null)
  69.                     con.close();
  70.                con=null;
  71.           }catch(Exception e){
  72.                e.printStackTrace();
  73.           }
  74.      }
  75.      public static void main(String[] args) throws Exception
  76.        {
  77.           Connect myDbTest = new Connect();
  78.           myDbTest.displayDbProperties();
  79.        }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement