document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. import java.sql.Connection;  
  2.  
  3. public class ConDb{  
  4.      private java.sql.Connection  con = null;  
  5.      private final String url = "jdbc:microsoft:sqlserver://";  
  6.      private final String serverName= "localhost";  
  7.      private final String portNumber = "1433";  
  8.      private final String databaseName= "dataBase";  
  9.      private final String userName = "sa";  
  10.      private final String password = "senha";
  11.  
  12.      // Informs the driver to use server a side-cursor,  
  13.      // which permits more than one active statement  
  14.      // on a Connection.  
  15.      private final String selectMethod = "cursor";  
  16.        
  17.      // Constructor  
  18.      public ConDb(){}  
  19.        
  20.      private String getConnectionUrl(){  
  21.           return url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";  
  22.      }
  23.  
  24.      public void getConnection(){  
  25.           try{  
  26.                Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");  
  27.                con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);  
  28.           }catch(Exception e){  
  29.                e.printStackTrace();  
  30.                System.out.println("Error Trace in getConnection() : " + e.getMessage());  
  31.          }  
  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.  
  76. }
');