Yurttas/PL/DBL/postgres/F/02/00/01/04/DBMeta00.java
Jump to navigation
Jump to search
1/**
2 * Copyright(C) 2005
3 * All Rights Reserved. Salih Yurttas, ZCubes, BitsOfCode Software Systems, Inc..
4 *
5 * Permission to use, copy, modify, and distribute this
6 * software and its documentation for EDUCATIONAL purposes
7 * and without fee is hereby granted provided that this
8 * copyright notice appears in all copies.
9 *
10 * @date : February 1, 2005.
11 * @author : Salih Yurttas.
12 */
13
14
15import java.sql.*;
16import java.net.*;
17
18import java.util.*;
19
20public class DBMeta00 {
21
22 protected static Connection con;
23
24 private static String driverClass,
25 url,
26 suffix;
27
28 public DBMeta00() {
29 driverClass = "com.mysql.jdbc.Driver";
30 url = "jdbc:mysql:";
31 suffix = "//database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank";
32 }
33
34 public boolean connect(String username,
35 String password)
36 throws SQLException
37 {
38 try {
39 Class.forName(driverClass);
40 con = DriverManager.getConnection(url+suffix,
41 username,
42 password);
43 if(con.isClosed()) return false;
44 }
45 catch(ClassNotFoundException cnfe) {
46 throw new SQLException("The mySQL JDBC Driver could not be loaded.");
47 }
48
49 return true;
50 }
51
52 public static boolean disconnect(Connection con)
53 throws SQLException
54 {
55 con.close();
56 return true;
57 }
58
59 public static boolean disconnect()
60 throws SQLException
61 {
62 con.close();
63 return true;
64 }
65
66 public static Vector getTableNames(String query)
67 throws SQLException
68 {
69 Vector tables = new Vector();
70
71 try {
72 Statement st = con.createStatement();
73
74 ResultSet rs = st.executeQuery(query);
75
76 ResultSetMetaData metaData = rs.getMetaData();
77 while(rs.next())
78 tables.addElement(rs.getString(1));
79 }
80 catch(SQLException sqle) {
81 System.out.println("Error in Show tables: jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database.");
82 throw sqle;
83 }
84
85 return tables;
86 }
87
88 public static void main (String[] args) {
89 String username = args[0];
90 String password = args[1];
91
92 DBMeta00 dbc = new DBMeta00();
93
94 try {
95 if(dbc.connect(username, password)) {
96 System.out.println("Connected to jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database...");
97
98 String query = "SHOW TABLES;";
99
100 Vector tables = getTableNames(query);
101 int n = tables.size();
102 System.out.println("Bank Tables :");
103 for(int i=0; i<n; i++)
104 System.out.println((String)tables.elementAt(i));
105
106 if(dbc.disconnect())
107 System.out.println("Disconnected from jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database.");
108 }
109 else System.out.println("Could not connect to jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database.");
110 }
111 catch(SQLException sqle) {
112 System.err.println("Error accessing jdbc:mysql://database.cs.BitsOfCode Software Systems, Inc..edu/yurttas-Bank database.");
113 System.err.println(sqle.getMessage());
114 }
115 }
116
117}