The Java Program: Access.java

  1 import java.awt.*;
  2 import java.awt.event.*;
  3 import java.sql.*;
  4 
  5 public class Access extends Frame {
  6 
  7    final TextField driver = new TextField(60);
  8    final TextField url = new TextField(60);
  9    final TextField sql = new TextField(60);
 10    final Button doIt = new Button("Execute");
 11    final TextArea resultArea = new TextArea(10, 60);
 12 
 13 
 14    public static void main (String args[]) {
 15       Access app = new Access();
 16    }
 17 
 18    public Access() {
 19       super("Access Application");
 20       setup();
 21       pack();
 22       addWindowListener(new WindowEventHandler());
 23       show();
 24    }
 25 
 26    void setup() {
 27       setupMenuBar();
 28       setLayout(new GridLayout(2,1));
 29       final Panel topPanel = new Panel();
 30       topPanel.setLayout(new GridLayout(4,1));
 31       final Panel panels[] = new Panel[4];
 32       for(int i=0;i<panels.length;++i){
 33          panels[i] = new Panel();
 34          panels[i].setLayout(new FlowLayout(FlowLayout.LEFT));
 35       }
 36 
 37       panels[0].add(new Label("Driver:"));
 38       panels[0].add(driver);
 39       panels[1].add(new Label("URL: "));
 40       panels[1].add(url);
 41       panels[2].add(new Label("SQL: "));
 42       panels[2].add(sql);
 43       doIt.addActionListener(new ButtonHandler());
 44       panels[3].add(doIt);
 45       for(int i=0;i<panels.length;++i)
 46          topPanel.add(panels[i]);
 47       add(topPanel);
 48       add(resultArea);
 49    }
 50 
 51    void setupMenuBar() {
 52       final MenuBar menuBar = new MenuBar();
 53       final Menu fileMenu = new Menu("File");
 54       final MenuItem fileExit = new MenuItem("Exit");
 55       fileExit.addActionListener(new MenuItemHandler());
 56       fileMenu.add(fileExit);
 57       menuBar.add(fileMenu);
 58       setMenuBar(menuBar);
 59    }
 60 
 61    void accessDB() {
 62       try{
 63          Class.forName(driver.getText());
 64          // Connect to database
 65          final Connection connection=DriverManager.getConnection(url.getText());
 66          final Statement statement = connection.createStatement();
 67 
 68          // Execute SQL
 69          final boolean hasResults = statement.execute(sql.getText());
 70 
 71          if (hasResults){
 72             // Get results of Query
 73             final ResultSet result = statement.getResultSet();
 74             if (result!=null) displayResults(result);
 75          } else {
 76             resultArea.setText("");
 77          }
 78          statement.close();
 79          connection.close();
 80       } catch (SQLException e) {
 81          resultArea.setText ("SQLException: " + e.getMessage() + "\n");
 82          resultArea.append  ("SQLState:     " + e.getSQLState() + "\n");
 83          resultArea.append  ("VendorError:  " + e.getErrorCode() + "\n");
 84       } catch (Exception ex){
 85          ex.printStackTrace();
 86          resultArea.setText(ex.toString());
 87       }
 88    }
 89 
 90    void displayResults (final ResultSet r) throws SQLException {
 91       final ResultSetMetaData rmeta = r.getMetaData();
 92 
 93       // Use the meta data to obtain the number of columns
 94       final int numColumns=rmeta.getColumnCount();
 95       String text="";
 96       for (int i=1;i<=numColumns;++i) {
 97          text += rmeta.getColumnName(i);
 98          if(i<numColumns) text += " | ";
 99       }
100       text+="\n";
101       while (r.next()){
102          for(int i=1;i<=numColumns;++i) {
103             final String s = r.getString(i);
104             if (s==null) {
105                text += "NULL";
106             } else {
107                text+=s.trim();
108             }
109             if(i<numColumns) text += " | ";
110          }
111          text+="\n";
112       }
113       resultArea.setText(text);
114    }
115 
116    class ButtonHandler implements ActionListener {
117       public void actionPerformed(ActionEvent ev){
118          final String s=ev.getActionCommand();
119          if (s=="Execute") accessDB();
120       }
121    }
122 
123    class MenuItemHandler implements ActionListener {
124       public void actionPerformed(ActionEvent ev) {
125          String s=ev.getActionCommand();
126          if(s=="Exit"){
127             System.exit(0);
128          }
129       }
130    }
131 
132    class WindowEventHandler extends WindowAdapter {
133       public void windowClosing(WindowEvent e) {
134          System.exit(0);
135       }
136    }
137 }