Advertisement
Guest User

Untitled

a guest
May 16th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 KB | None | 0 0
  1. package olioprojekti;
  2.  
  3. //Imports are listed in full to show what's being used
  4. //could just import javax.swing.* and java.awt.* etc..
  5. import javax.swing.JFrame;
  6. import javax.swing.JPanel;
  7. import javax.swing.JComboBox;
  8. import javax.swing.JButton;
  9. import javax.swing.JLabel;
  10. import javax.swing.JList;
  11. import javax.swing.JScrollPane;
  12. import javax.swing.JTextArea;
  13. import javax.swing.JTextField;
  14.  
  15. import java.awt.BorderLayout;
  16. import java.awt.FlowLayout;
  17. import java.awt.event.ActionListener;
  18. import java.awt.event.ActionEvent;
  19. import java.sql.*;
  20. public class GuiApp1 {
  21.    
  22.     //Note: Typically the main method will be in a
  23.     //separate class. As this is a simple one class
  24.     //example it's all in the one class.
  25.     public static void main(String[] args) {
  26.         new GuiApp1();
  27.        
  28.         String query = "select *" +
  29.                 "from 'CHARACTER' , ITEM, NPC, QUESTS";
  30.        
  31.         try{
  32.             Class.forName("com.mysql.jdbc.Driver");
  33.         } catch (Exception e) {
  34.             System.out.println("Error: Database driver: " + e);
  35.             System.exit(1); // lopetetaan ohjelma
  36.         }
  37.        
  38.         String host = "jdbc:mysql://mysql.cc.puv.fi:3306/e1401175_projekti";
  39.         String uName = "e1401175";
  40.         String uPass= "jnCgtUXV7jZf";
  41.        
  42.         try {
  43.             Connection con = DriverManager.getConnection(host, uName, uPass);
  44.              
  45.             try{
  46.                 Statement stm = con.createStatement();
  47.                 ResultSet rs = stm.executeQuery(query);
  48.             }
  49.             catch(SQLException e) {
  50.                 System.out.println("Error in query!");
  51.             }
  52.         }
  53.         catch ( SQLException err ) {
  54.         System.out.println( err.getMessage( ) );
  55.         }
  56.     }
  57.  
  58.     public GuiApp1()
  59.     {
  60.         JFrame guiFrame = new JFrame();
  61.        
  62.         //make sure the program exits when the frame closes
  63.         guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  64.         guiFrame.setTitle("RPG Database Checker");
  65.         guiFrame.setSize(500,500);
  66.      
  67.         //This will center the JFrame in the middle of the screen
  68.         guiFrame.setLocationRelativeTo(null);
  69.        
  70.         //Options for the JComboBox
  71.         String[] tableOptions = {"Characters", "Quests", "Items", "NPCs"};
  72.        
  73.        
  74.         //The first JPanel contains a JLabel and JCombobox
  75.         final JPanel comboPanel = new JPanel();
  76.         final JPanel listPanel = new JPanel();
  77.         final JPanel infoPanel = new JPanel();
  78.         final JPanel allInfoPanel = new JPanel();
  79.         JLabel comboLbl = new JLabel("Print a table:");
  80.         JComboBox tables = new JComboBox(tableOptions);
  81.         JTextArea textArea = new JTextArea(5, 5);
  82.         JScrollPane scrollPane = new JScrollPane(textArea);
  83.         JLabel listLbl = new JLabel("Contents:");
  84.         JTextArea textArea2 = new JTextArea(20, 35);
  85.         JScrollPane scrollPane2 = new JScrollPane(textArea2);
  86.         JButton printAllButton = new JButton("This prints all info");
  87.  
  88.         final String newline = "\n";
  89.        
  90.         textArea.setEditable(false);
  91.         textArea2.setEditable(false);
  92.         //comboPanel
  93.         comboPanel.add(comboLbl);
  94.         comboPanel.add(tables);
  95.         //infoPanel
  96.         String text = "This will contain the info you chose";
  97.         textArea.append(text + newline);
  98.         infoPanel.add(textArea);
  99.         //listPanel
  100.         listPanel.add(listLbl);
  101.         listPanel.setVisible(false);
  102.         //allInfoPanel
  103.         String text2 = "This will contain all table info";
  104.         textArea2.append(text2 + newline);
  105.         allInfoPanel.add(textArea2);
  106.         allInfoPanel.setVisible(false);
  107.  
  108.         //The ActionListener class is used to handle the
  109.         //event that happens when the user clicks the button.
  110.         //As there is not a lot that needs to happen we can
  111.         //define an anonymous inner class to make the code simpler.
  112.         printAllButton.addActionListener(new ActionListener()
  113.         {
  114.             @Override
  115.             public void actionPerformed(ActionEvent event)
  116.             {
  117.                //When the button is pressed
  118.                //the setVisible value of the listPanel and
  119.                //comboPanel is switched from true to
  120.                //value or vice versa.
  121.                
  122.                 String query = "SELECT *" + "FROM ´character´";
  123.                 String host = "jdbc:mysql://mysql.cc.puv.fi:3306/e1401175_projekti";
  124.                 String uName = "e1401175";
  125.                 String uPass= "jnCgtUXV7jZf";
  126.                
  127.                 try {
  128.                     Connection con = DriverManager.getConnection(host, uName, uPass);
  129.                      
  130.                     try{
  131.                         PreparedStatement stm = con.prepareStatement(query);
  132.                         ResultSet rs = stm.executeQuery(query);
  133.                        
  134.                         StringBuilder strBuilder = new StringBuilder();
  135.                         while(rs.next()) {
  136.                             strBuilder.append(rs.getString(1)).append(" ").append(rs.getString(2));
  137.                             strBuilder.append("\n");
  138.                         }
  139.                         textArea.setText(strBuilder.toString());
  140.                         con.close();
  141.                     }
  142.                     catch(SQLException e) {
  143.                         System.out.println("Error in query!");
  144.                     }
  145.                 }
  146.                 catch ( SQLException err ) {
  147.                 System.out.println( err.getMessage( ) );
  148.                 }
  149.                
  150.                listPanel.setVisible(!listPanel.isVisible());
  151.                infoPanel.setVisible(!infoPanel.isVisible());
  152.                comboPanel.setVisible(!comboPanel.isVisible());
  153.                allInfoPanel.setVisible(!allInfoPanel.isVisible());
  154.             }
  155.          }
  156.         );
  157.        
  158.         //The JFrame uses the BorderLayout layout manager.
  159.         //Put the two JPanels and JButton in different areas.
  160.         guiFrame.add(comboPanel, BorderLayout.CENTER);
  161.         guiFrame.add(infoPanel, BorderLayout.CENTER);
  162.         guiFrame.add(listPanel, BorderLayout.NORTH);
  163.         guiFrame.add(allInfoPanel, BorderLayout.CENTER);
  164.         guiFrame.add(printAllButton,BorderLayout.SOUTH);
  165.        
  166.         //make sure the JFrame is visible
  167.         guiFrame.setVisible(true);
  168.     }
  169.    
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement