Advertisement
Guest User

Untitled

a guest
May 21st, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.35 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import javax.swing.JApplet;
  4. import javax.swing.JFrame;
  5. import javax.swing.*;
  6. import javax.swing.SwingUtilities;
  7. import javax.swing.tree.DefaultMutableTreeNode;
  8. import java.sql.*;
  9. import java.util.Properties;
  10.  
  11. public class RevisionTreeEngine extends JPanel implements ActionListener
  12. {
  13.     private static final String dbconnection = "com.mysql.jdbc.Driver";
  14.     private static final String URL          = "jdbc:mysql://127.0.0.1:3306/cs205";
  15.     private static final String REFRESH_CMD  = "refresh";
  16.     private static final String REFRESH_LBL  = "Refresh/Update";
  17.     private static final int    SIZE_X       = 300;
  18.     private static final int    SIZE_Y       = 150;
  19.     private JPanel       menuPanel;
  20.     private JButton      refresh_btn;
  21.     private RevisionTree treePanel;
  22.     private Connection   conn;
  23.  
  24.     public RevisionTreeEngine()
  25.     {
  26.         super(new BorderLayout());
  27.         treePanel   = new RevisionTree();
  28.         menuPanel   = new JPanel(new GridLayout(0,3));
  29.  
  30.         refresh_btn = new JButton(REFRESH_LBL);
  31.         treePanel.setPreferredSize(new Dimension(SIZE_X, SIZE_Y));
  32.         add(treePanel, BorderLayout.CENTER);
  33.  
  34.         refresh_btn.setActionCommand(REFRESH_CMD);
  35.         refresh_btn.addActionListener(this);
  36.         menuPanel.add(refresh_btn);
  37.         add(menuPanel, BorderLayout.SOUTH);
  38.  
  39.         openConnection();
  40.         refreshTree();
  41.     }
  42.  
  43.  
  44.     private void openConnection()
  45.     {
  46.         try {
  47.             Properties p = new Properties();
  48.             p.put("user", "******");
  49.             p.put("password","******");
  50.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  51.             conn =  DriverManager.getConnection(URL, p);
  52.         } catch (ClassNotFoundException ex) {
  53.             System.err.println(ex.getMessage());
  54.     } catch (IllegalAccessException ex) {
  55.             System.err.println(ex.getMessage());
  56.         } catch (InstantiationException ex) {
  57.             System.err.println(ex.getMessage());
  58.         } catch (SQLException ex) {
  59.             System.err.println(ex.getMessage());
  60.             System.out.println("Failed in SQLException");
  61.         }
  62.     }
  63.  
  64.     private void refreshTree()
  65.     {
  66.         try {
  67.             treePanel.clear();
  68.             String query =  "select * from project";
  69.             System.out.println("Getting here 3");
  70.             Statement st = conn.createStatement();
  71.             System.out.println("Getting here 4");
  72.             ResultSet rs = st.executeQuery(query);
  73.             while (rs.next()) {
  74.                 int id = rs.getInt("id");
  75.                 int uid = rs.getInt("uid");
  76.                 double lng = rs.getFloat("lng");
  77.                 double lat = rs.getFloat("lat");
  78.                 String desc = "Node " + id + " by user " + uid + " at lng/lat: " + lng + "/" + lat + "";
  79.                 treePanel.addObject(null, desc);
  80.             }
  81.             st.close();
  82.         } catch (SQLException ex) {
  83.             System.err.println(ex.getMessage());
  84.             System.out.println("Failed in SQLException");
  85.         }
  86.     }
  87.  
  88.     private static void createAndShowGUI()
  89.     {
  90.         //Create and set up the window.
  91.      
  92.         JFrame frame = new JFrame("RevisionTreeEngine");
  93.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  94.  
  95.         //Create and set up the content pane.
  96.                                                                                                                      
  97.         RevisionTreeEngine newContentPane = new RevisionTreeEngine();
  98.         newContentPane.setOpaque(true); //content panes must be opaque
  99.                                                                                              
  100.         frame.setContentPane(newContentPane);
  101.  
  102.         //Display the window.
  103.                                                                                                                               frame.pack();
  104.         frame.setVisible(true);
  105.     }
  106.  
  107.  
  108.     // called when run as an application    
  109.     /*
  110.     public static void main(String[] args)
  111.     {
  112.         //Schedule a job for the event-dispatching thread:
  113.         //creating and showing this application's GUI.
  114.     javax.swing.SwingUtilities.invokeLater(new Runnable()
  115.     {
  116.         public void run()
  117.         {
  118.             createAndShowGUI();
  119.         }
  120.     });
  121.     }
  122.     */
  123. }
  124.  
  125. //========================
  126.  
  127. import java.applet.*;
  128. import java.awt.*;
  129. import java.awt.event.*;
  130. import javax.swing.JFrame;
  131. import javax.swing.JApplet;
  132. import javax.swing.SwingUtilities;
  133. import java.sql.*;
  134. import java.util.Properties;
  135. import javax.swing.tree.DefaultMutableTreeNode;
  136.  
  137. public class RevisionTreeApplet extends JApplet
  138. {
  139.     private RevisionTreeEngine  app;
  140.  
  141.     public void init()
  142.     {
  143.         //Execute a job on the event-dispatching thread; creating this applet's GUI.
  144.  
  145.         try {
  146.             SwingUtilities.invokeAndWait(new Runnable()
  147.                 {
  148.                     public void run()
  149.                     {
  150.                         createGUI();
  151.                     }
  152.                 });
  153.         } catch (Exception e) {
  154.             System.err.println("createGUI didn't complete successfully");
  155.         }
  156.     }
  157.  
  158.     private void createGUI()
  159.     {
  160.         //Create and set up the content pane.
  161.  
  162.         app = new RevisionTreeEngine();
  163.         app.setOpaque(true);
  164.         setContentPane(app);
  165.     }
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement