1 package org.melati.poem.dbms.test;
2
3 import java.io.PrintWriter;
4 import java.sql.Driver;
5 import java.sql.DriverManager;
6
7
8
9
10
11
12 public class DebugSQLServerConnection {
13 private java.sql.Connection con = null;
14
15 private final String url = "jdbc:sqlserver://";
16
17 private final String serverName = "localhost";
18
19 private final String portNumber = "1433";
20
21 private final String databaseName = "melatijunit";
22
23 private final String userName = "sa";
24
25 private final String password = "";
26
27
28
29
30
31
32
33
34
35 public DebugSQLServerConnection() {
36 }
37
38 private String getConnectionUrl() {
39 return url + serverName + ":" + portNumber + ";databaseName="
40 + databaseName + ";selectMethod=cursor;";
41 }
42
43 private java.sql.Connection getConnection() {
44 try {
45 Class<?> driverClass = Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
46 if (driverClass == null)
47 throw new RuntimeException("No class found");
48 Driver driver = (Driver) driverClass.newInstance();
49
50 DriverManager.setLogWriter(new PrintWriter(System.out));
51 DriverManager.registerDriver(driver);
52
53 con = java.sql.DriverManager.getConnection(getConnectionUrl(), userName,
54 password);
55 if (con != null)
56 System.out.println("Connection Successful!");
57 } catch (Exception e) {
58 e.printStackTrace();
59 System.out.println("Error Trace in getConnection() : " + e.getMessage());
60 }
61 return con;
62 }
63
64
65
66
67 public void displayDbProperties() {
68 java.sql.DatabaseMetaData dm = null;
69 java.sql.ResultSet rs = null;
70 try {
71 con = this.getConnection();
72 if (con != null) {
73 dm = con.getMetaData();
74 System.out.println("Driver Information");
75 System.out.println("\tDriver Name: " + dm.getDriverName());
76 System.out.println("\tDriver Version: " + dm.getDriverVersion());
77 System.out.println("\nDatabase Information ");
78 System.out.println("\tDatabase Name: " + dm.getDatabaseProductName());
79 System.out.println("\tDatabase Version: "
80 + dm.getDatabaseProductVersion());
81 System.out.println("Avalilable Catalogs ");
82 rs = dm.getCatalogs();
83 while (rs.next()) {
84 System.out.println("\tcatalog: " + rs.getString(1));
85 }
86 rs.close();
87 rs = null;
88 closeConnection();
89 } else
90 System.out.println("Error: No active Connection");
91 } catch (Exception e) {
92 e.printStackTrace();
93 }
94 dm = null;
95 }
96
97 private void closeConnection() {
98 try {
99 if (con != null)
100 con.close();
101 con = null;
102 } catch (Exception e) {
103 e.printStackTrace();
104 }
105 }
106
107
108
109
110
111
112 public static void main(String[] args) throws Exception {
113 DebugSQLServerConnection myDbTest = new DebugSQLServerConnection();
114 myDbTest.displayDbProperties();
115 }
116 }