Yurttas/PL/DBL/mysql-f-2010/02/00/01/04/dbops/DBTable.java

 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 2005, April 2010.
11 *  @authorĀ : Salih Yurttas.
12 */
13
14
15package dbops;
16
17import java.util.*;
18
19import java.sql.*;
20
21public class DBTable {
22
23  private Connection con;
24
25  public DBTable(final Connection con)
26    throws SQLException
27  {
28    this.con = con;
29  }
30
31  public boolean setStatement(final String table) 
32    throws SQLException
33  {
34    Statement st = con.createStatement();
35    st.executeUpdate(table);
36    return true;
37  }
38
39  public ResultSet getStatement(final String query) 
40    throws SQLException
41  {
42    Statement st = con.createStatement();
43    ResultSet rSet = st.executeQuery(query);
44    return rSet;
45  }
46
47  public ArrayList<String> getTableNames(final String query)
48    throws SQLException
49  {
50    ArrayList<String> tables = new ArrayList<String>();
51
52    try {
53      Statement st = con.createStatement();
54
55      ResultSet rs = st.executeQuery(query);
56
57      ResultSetMetaData metaData = rs.getMetaData();
58      while(rs.next())
59        tables.add(rs.getString(1));
60    }
61    catch(SQLException sqle) {
62      System.out.println("Error in getTableNames");
63      throw sqle;
64    }
65
66    return tables;
67  }
68
69  public ArrayList<String> getColumnNames(final String query)
70    throws SQLException
71  {
72    ArrayList<String> columns = new ArrayList<String>();
73
74    try {
75      Statement st = con.createStatement();
76
77      ResultSet rs = st.executeQuery(query);
78
79      ResultSetMetaData metaData = rs.getMetaData();
80
81      int columnCount = metaData.getColumnCount();
82      for(int i=1; i<=columnCount; ++i)
83        columns.add(metaData.getColumnName(i));
84    }
85    catch(SQLException sqle) {
86      System.out.println("Error in getColumnNames");
87      throw sqle;
88    }
89
90    return columns;
91  }
92
93}