Results 1 to 1 of 1
- 10-12-2012, 04:03 AM #1
Member
- Join Date
- Oct 2012
- Posts
- 1
- Rep Power
- 0
Database monitor and general theory help!
I'm trying to teach myself Java (for the past week). Anyway, I have a JFrame with a six text areas (each inside a scrollpane). Each text area is populated by running a query on a local access db and posting the results to that area. Currently, to do this, I just have a refresh button that, when clicked, goes to an event which calls all of the individual private voids that query / update the text areas. The source is like this (I'm going to leave some of the bulk out):
This is the code I have been trying... It works, but I can't figure out how to implement a swing timer. I would like to be able to set an amount of time (2 min, 5 min, etc) and have the program auto-update from the database. Right now I use a refresh button. I want to make this program a lot better (implement custom database queries, run reports on the database, etc...), but I don't really "know" Java. Do the individual parts that update each text area need to go in separate classes? I'm a bit lost.
Java Code:import java.sql.*; import java.util.Set; import javax.swing.JOptionPane; import javax.swing.JTable; import javax.swing.Timer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import java.awt.event.*; public class DBMonitor extends javax.swing.JFrame { public DBMonitor() { initComponents(); } private void RefreshButtonActionPerformed(java.awt.event.ActionEvent evt) { UpdateProdAreaOne(); UpdateProdAreaTwo(); UpdateProdAreaThree(); UpdateProdAreaFour(); UpdateProdAreaFive(); UpdateProdAreaSix(); } //There are five other private voids like this but it seemed silly to put them all in here. private void UpdateProdAreaOne() { try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); String url = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};" + "DBQ=C:\\TEST4.accdb"; Connection con = DriverManager.getConnection(url); Statement stmt = null; ResultSet rs = null; String sBuf = ""; String SQL = "SELECT * FROM Test_Track WHERE `Start ID` is NOT NULL and `End ID` is NULL"; stmt = con.createStatement(); rs = stmt.executeQuery(SQL); while (rs.next()) { sBuf = sBuf + rs.getString("ID") + " : " + rs.getString("Type") + "\n"; } AreaOne_TextArea.setText(sBuf); con.close(); } catch (SQLException e) { System.out.println("SQL Exception: " + e.toString()); DBMonitor.errorBox("SQL Exception: " + e.toString(), "SQL Error"); } catch (ClassNotFoundException cE) { System.out.println("Class Not Found Exception: " + cE.toString()); DBMonitor.errorBox("Class Not Found Exception: " + cE.toString(), "Class Not Found Exception"); } } public static void infoBox(String infoMessage, String location) { JOptionPane.showMessageDialog(null, infoMessage, location, JOptionPane.INFORMATION_MESSAGE); } public static void errorBox(String infoMessage, String location) { JOptionPane.showMessageDialog(null, infoMessage, location, JOptionPane.ERROR_MESSAGE); public static void main(String args[]) { try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(DBMonitor.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } //</editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new DBMonitor().setVisible(true); } }); } //Variable Declarations //...no need to post unless wanted
Similar Threads
-
Check my JAVA theory HW?
By tripline in forum New To JavaReplies: 5Last Post: 10-28-2011, 01:04 AM -
General info about communication between servlet and database
By vagf in forum Java ServletReplies: 0Last Post: 04-23-2011, 05:15 PM -
OO Class Theory Question
By Dark in forum New To JavaReplies: 22Last Post: 04-20-2011, 02:16 PM -
Minimax game theory with mancala
By jigglywiggly in forum New To JavaReplies: 2Last Post: 01-01-2010, 12:04 PM -
[SOLVED] General string problem in my database query.....?
By prabhurangan in forum New To JavaReplies: 7Last Post: 06-25-2008, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks