public void actionPerformed(ActionEvent event) { // if clear button is pressed, clear all fields if (event.getSource() == clear_button) { // erase all fields taskname_field.setText(""); taskdesc_field.setText(""); priority_checkbox.setSelected(false); numberofdays_field.setText(""); // isallocatedcheckbox.setSelected(false); duedate_field.setText(""); } // if submit button is pressed, save all data onto database if (event.getSource() == submit_button) { try { { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); // set to current date calendar.add( Calendar.DAY_OF_MONTH, Integer.parseInt(this.numberofdays_field.getText()) - 1); // add days required... then: // ^ subtract one day, because if task is created today and allocated tomorrow for one // day, // then it should be done by end of day tomorrow Date date = calendar.getTime(); // (see next *1) use this date to be the earliest // because of expected task duration /*This is a constraint * the date and the length of days that user expected to take * if not feasible then the program will say so * */ // method compares the current date and the date that was entered by the user if (this.task_idfield.getText().length() != 7 // check chars || this.taskname_field.getText().length() > 50 // check chars || this.taskdesc_field.getText().length() > 50 || ((Date) this.duedate_field.getValue()).compareTo(date) < 0 // check that the date is possible (see last *1) || this.numberofdays_field.getText().length() > 2 || this.numberofdays_field.getText().length() == 0 // check number of digits || Integer.parseInt(this.numberofdays_field.getText()) < 1) // check number of days required for task { JOptionPane.showMessageDialog( this, "Task ID has to be 7 chars; " + "\n" + "Description <= 50 chars; " + "\n" + "Because the expected duration of the task is " + Integer.parseInt(this.numberofdays_field.getText()) + " days, the due date cannot be earlier than: " + new StringBuffer(new SimpleDateFormat("dd/MM/yyyy").format(date)).toString() + "; " + "\n" + "Task duration must be 2 digit number maximum and must be an integer greater than 0"); } else // check if taskid_field is equals to any of the fields on the database if (task_idfield.equals("")) { JOptionPane.showMessageDialog( this, "Please insert an identification number for a task"); } else // add constraint and tell customer to enter 7 values if (taskname_field.equals("")) { JOptionPane.showMessageDialog(this, "Please insert a name to identity the task"); } else { // get all inputs from user inputs and store in variables taskid_input = task_idfield.getText(); taskname_input = taskname_field.getText(); taskdesc_input = taskdesc_field.getText(); if (priority_checkbox.isSelected()) { priority_input = 1; } else { priority_input = 0; } // create and store date from user input date = (Date) duedate_field.getValue(); // store number of days input from user numberofdays_input = Integer.parseInt(numberofdays_field.getText()); // String[] column_names = { "Task ID", "Task Name", "Task Description", "Task // Priority", "Due Date", "Number of Days", "Is Allocated" , "Unallocateable" }; task = new Object[8]; task[0] = taskid_input; task[1] = taskname_input; task[2] = taskdesc_input; task[3] = priority_input; task[4] = date; task[5] = numberofdays_input; task[6] = 0; task[7] = 0; try { // SqlConnection.connect(); SqlConnection.ps = SqlConnection.connection.prepareStatement( "INSERT INTO TASK VALUES (?,?,?,?,?,?,?,?)"); SqlConnection.ps.setString(1, taskid_input); SqlConnection.ps.setString(2, taskname_input); SqlConnection.ps.setString(3, taskdesc_input); SqlConnection.ps.setInt(4, priority_input); SqlConnection.ps.setDate(5, new java.sql.Date(date.getTime())); SqlConnection.ps.setInt(6, numberofdays_input); SqlConnection.ps.setInt(7, 0); SqlConnection.ps.setInt(8, 0); SqlConnection.ps.executeUpdate(); // now enter skills into TASK SKILL database for (int i = 0; i < skill_list.size(); i++) { if (skill_list.get(i).isSelected()) { String skillid = skill_list.get(i).getText(); SqlConnection.statement.executeUpdate( "INSERT INTO TASKSKILL VALUES ('" + taskid_input + "', '" + skillid + "')"); } } SqlConnection.ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } // SqlConnection.ps.close(); SqlConnection.closeConnection(); this.setVisible(false); } } } catch (Exception exc) { JOptionPane.showMessageDialog( this, "Check your inputs:" + "\n" + "Priority has to be a 1-digit number;" + "\n" + "Date has to be in the format dd/MM/yyyy;" + "\n" + "Task length has to be 2-digit number"); System.out.println(exc); } } // display dialog box asking task manager if they want to quit, // if task manager does not want to quit, go back to adding more tasks // if customer quits, close the dialog box and the frame // now close the window after saving }
private void execute() { gResult.clear(); String sCmd = null; if (4096 <= ifHuge.length()) { sCmd = ifHuge; } else { sCmd = txtCommand.getText(); } if (sCmd.startsWith("-->>>TEST<<<--")) { testPerformance(); return; } String g[] = new String[1]; try { lTime = System.currentTimeMillis(); sStatement.execute(sCmd); int r = sStatement.getUpdateCount(); if (r == -1) { formatResultSet(sStatement.getResultSet()); } else { g[0] = "update count"; gResult.setHead(g); g[0] = "" + r; gResult.addRow(g); } lTime = System.currentTimeMillis() - lTime; addToRecent(txtCommand.getText()); gResult.fireTableChanged(null); } catch (SQLException e) { lTime = System.currentTimeMillis() - lTime; g[0] = "SQL Error"; gResult.setHead(g); String s = e.getMessage(); s += " / Error Code: " + e.getErrorCode(); s += " / State: " + e.getSQLState(); g[0] = s; gResult.addRow(g); gResult.fireTableChanged(null); } updateResult(); System.gc(); }
private void testPerformance() { String all = txtCommand.getText(); StringBuffer b = new StringBuffer(); long total = 0; for (int i = 0; i < all.length(); i++) { char c = all.charAt(i); if (c != '\n') { b.append(c); } } all = b.toString(); String g[] = new String[4]; g[0] = "ms"; g[1] = "count"; g[2] = "sql"; g[3] = "error"; gResult.setHead(g); int max = 1; lTime = System.currentTimeMillis() - lTime; while (!all.equals("")) { int i = all.indexOf(';'); String sql; if (i != -1) { sql = all.substring(0, i); all = all.substring(i + 1); } else { sql = all; all = ""; } if (sql.startsWith("--#")) { max = Integer.parseInt(sql.substring(3)); continue; } else if (sql.startsWith("--")) { continue; } g[2] = sql; long l = 0; try { l = DatabaseManagerCommon.testStatement(sStatement, sql, max); total += l; g[0] = "" + l; g[1] = "" + max; g[3] = ""; } catch (SQLException e) { g[0] = g[1] = "n/a"; g[3] = e.toString(); } gResult.addRow(g); System.out.println(l + " ms : " + sql); } g[0] = "" + total; g[1] = "total"; g[2] = ""; gResult.addRow(g); lTime = System.currentTimeMillis() - lTime; updateResult(); }
public void actionPerformed(ActionEvent ev) { String s = ev.getActionCommand(); if (s == null) { if (ev.getSource() instanceof JMenuItem) { JMenuItem i; s = ((JMenuItem) ev.getSource()).getText(); } } /* // button replace by toolbar if (s.equals("Execute")) { execute(); } else */ if (s.equals("Exit")) { windowClosing(null); } else if (s.equals("Transfer")) { Transfer.work(null); } else if (s.equals("Dump")) { Transfer.work(new String[] {"-d"}); } else if (s.equals("Restore")) { Transfer.work(new String[] {"-r"}); } else if (s.equals("Logging on")) { javaSystem.setLogToSystem(true); } else if (s.equals("Logging off")) { javaSystem.setLogToSystem(false); } else if (s.equals("Refresh Tree")) { refreshTree(); } else if (s.startsWith("#")) { int i = Integer.parseInt(s.substring(1)); txtCommand.setText(sRecent[i]); } else if (s.equals("Connect...")) { connect(ConnectionDialogSwing.createConnection(fMain, "Connect")); refreshTree(); } else if (s.equals("Results in Grid")) { iResult = 0; pResult.removeAll(); pResult.add(gScrollPane, BorderLayout.CENTER); pResult.doLayout(); gResult.fireTableChanged(null); pResult.repaint(); } else if (s.equals("Open Script...")) { JFileChooser f = new JFileChooser("."); f.setDialogTitle("Open Script..."); // (ulrivo): set default directory if set from command line if (defDirectory != null) { f.setCurrentDirectory(new File(defDirectory)); } int option = f.showOpenDialog(fMain); if (option == JFileChooser.APPROVE_OPTION) { File file = f.getSelectedFile(); if (file != null) { StringBuffer buf = new StringBuffer(); ifHuge = DatabaseManagerCommon.readFile(file.getAbsolutePath()); if (4096 <= ifHuge.length()) { buf.append("This huge file cannot be edited. Please execute\n"); txtCommand.setText(buf.toString()); } else { txtCommand.setText(ifHuge); } } } } else if (s.equals("Save Script...")) { JFileChooser f = new JFileChooser("."); f.setDialogTitle("Save Script"); // (ulrivo): set default directory if set from command line if (defDirectory != null) { f.setCurrentDirectory(new File(defDirectory)); } int option = f.showSaveDialog(fMain); if (option == JFileChooser.APPROVE_OPTION) { File file = f.getSelectedFile(); if (file != null) { DatabaseManagerCommon.writeFile(file.getAbsolutePath(), txtCommand.getText()); } } } else if (s.equals("Save Result...")) { JFileChooser f = new JFileChooser("."); f.setDialogTitle("Save Result..."); // (ulrivo): set default directory if set from command line if (defDirectory != null) { f.setCurrentDirectory(new File(defDirectory)); } int option = f.showSaveDialog(fMain); if (option == JFileChooser.APPROVE_OPTION) { File file = f.getSelectedFile(); if (file != null) { showResultInText(); DatabaseManagerCommon.writeFile(file.getAbsolutePath(), txtResult.getText()); } } } else if (s.equals("Results in Text")) { iResult = 1; pResult.removeAll(); pResult.add(txtResultScroll, BorderLayout.CENTER); pResult.doLayout(); showResultInText(); pResult.repaint(); } else if (s.equals("AutoCommit on")) { try { cConn.setAutoCommit(true); } catch (SQLException e) { } } else if (s.equals("AutoCommit off")) { try { cConn.setAutoCommit(false); } catch (SQLException e) { } } else if (s.equals("Commit")) { try { cConn.commit(); } catch (SQLException e) { } } else if (s.equals("Insert test data")) { insertTestData(); } else if (s.equals("Rollback")) { try { cConn.rollback(); } catch (SQLException e) { } } else if (s.equals("Disable MaxRows")) { try { sStatement.setMaxRows(0); } catch (SQLException e) { } } else if (s.equals("Set MaxRows to 100")) { try { sStatement.setMaxRows(100); } catch (SQLException e) { } } else if (s.equals("SELECT")) { showHelp(DatabaseManagerCommon.selectHelp); } else if (s.equals("INSERT")) { showHelp(DatabaseManagerCommon.insertHelp); } else if (s.equals("UPDATE")) { showHelp(DatabaseManagerCommon.updateHelp); } else if (s.equals("DELETE")) { showHelp(DatabaseManagerCommon.deleteHelp); } else if (s.equals("CREATE TABLE")) { showHelp(DatabaseManagerCommon.createTableHelp); } else if (s.equals("DROP TABLE")) { showHelp(DatabaseManagerCommon.dropTableHelp); } else if (s.equals("CREATE INDEX")) { showHelp(DatabaseManagerCommon.createIndexHelp); } else if (s.equals("DROP INDEX")) { showHelp(DatabaseManagerCommon.dropIndexHelp); } else if (s.equals("CHECKPOINT")) { showHelp(DatabaseManagerCommon.checkpointHelp); } else if (s.equals("SCRIPT")) { showHelp(DatabaseManagerCommon.scriptHelp); } else if (s.equals("SHUTDOWN")) { showHelp(DatabaseManagerCommon.shutdownHelp); } else if (s.equals("SET")) { showHelp(DatabaseManagerCommon.setHelp); } else if (s.equals("Test Script")) { showHelp(DatabaseManagerCommon.testHelp); } }