Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 28th, 2012  |  syntax: None  |  size: 3.02 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. JDBC install and configuration with JSE 6 and Tomcat 6 - help please
  2. import java.*;
  3. public class Connect
  4. {
  5.     private java.sql.Connection  con = null;
  6.     private final String url = "jdbc:sqlserver://";
  7.     private final String serverName= "localhost";
  8.     private final String portNumber = "1433";
  9.     private final String databaseName= "yourDatabaseName";
  10.     private final String userName = "yourUserName";
  11.     private final String password = "yourPassword";
  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 Connect(){}
  19.  
  20.     private String getConnectionUrl()
  21.     {
  22.         return   url+serverName+":"+portNumber+";databaseName="+databaseName+";selectMethod="+selectMethod+";";
  23.     }
  24.  
  25.     private java.sql.Connection getConnection()
  26.     {
  27.         try
  28.         {
  29.             Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
  30.             con = java.sql.DriverManager.getConnection(getConnectionUrl(),userName,password);
  31.             if(con!=null) System.out.println("Connection Successful!");
  32.         }
  33.         catch(Exception e)
  34.         {
  35.                e.printStackTrace();
  36.                System.out.println("Error Trace in getConnection() : " + e.getMessage());
  37.         }
  38.         return con;
  39.     }
  40.  
  41.     /*
  42.          Display the driver properties, database details
  43.     */
  44.  
  45.     public void displayDbProperties()
  46.     {
  47.         java.sql.DatabaseMetaData dm = null;
  48.         java.sql.ResultSet rs = null;
  49.         try
  50.         {
  51.             con= this.getConnection();
  52.             if(con!=null)
  53.             {
  54.                 dm = con.getMetaData();
  55.                 System.out.println("Driver Information");
  56.                 System.out.println("tDriver Name: "+ dm.getDriverName());
  57.                 System.out.println("tDriver Version: "+ dm.getDriverVersion ());
  58.                 System.out.println("nDatabase Information ");
  59.                 System.out.println("tDatabase Name: "+ dm.getDatabaseProductName());
  60.                 System.out.println("tDatabase Version: "+ dm.getDatabaseProductVersion());
  61.                 System.out.println("Avalilable Catalogs ");
  62.                 rs = dm.getCatalogs();
  63.                 while(rs.next())
  64.                 {
  65.                      System.out.println("tcatalog: "+ rs.getString(1));
  66.                 }
  67.                 rs.close();
  68.                 rs = null;
  69.                 closeConnection();
  70.                }else System.out.println("Error: No active Connection");
  71.         }
  72.         catch(Exception e)
  73.         {
  74.             e.printStackTrace();
  75.         }
  76.         dm=null;
  77.     }    
  78.  
  79.     private void closeConnection()
  80.     {
  81.         try
  82.         {
  83.             if(con!=null)
  84.             con.close();
  85.             con=null;
  86.         }
  87.         catch(Exception e)
  88.         {
  89.             e.printStackTrace();
  90.         }
  91.     }
  92.     public static void main(String[] args) throws Exception
  93.     {
  94.         Connect myDbTest = new Connect();
  95.         myDbTest.displayDbProperties();
  96.     }
  97. }