Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package olioprojekti;
- //Imports are listed in full to show what's being used
- //could just import javax.swing.* and java.awt.* etc..
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.JComboBox;
- import javax.swing.JButton;
- import javax.swing.JLabel;
- import javax.swing.JList;
- import javax.swing.JScrollPane;
- import javax.swing.JTextArea;
- import javax.swing.JTextField;
- import java.awt.BorderLayout;
- import java.awt.FlowLayout;
- import java.awt.event.ActionListener;
- import java.awt.event.ActionEvent;
- import java.sql.*;
- public class GuiApp1 {
- //Note: Typically the main method will be in a
- //separate class. As this is a simple one class
- //example it's all in the one class.
- public static void main(String[] args) {
- new GuiApp1();
- String query = "select *" +
- "from 'CHARACTER' , ITEM, NPC, QUESTS";
- try{
- Class.forName("com.mysql.jdbc.Driver");
- } catch (Exception e) {
- System.out.println("Error: Database driver: " + e);
- System.exit(1); // lopetetaan ohjelma
- }
- String host = "jdbc:mysql://mysql.cc.puv.fi:3306/e1401175_projekti";
- String uName = "e1401175";
- String uPass= "jnCgtUXV7jZf";
- try {
- Connection con = DriverManager.getConnection(host, uName, uPass);
- try{
- Statement stm = con.createStatement();
- ResultSet rs = stm.executeQuery(query);
- }
- catch(SQLException e) {
- System.out.println("Error in query!");
- }
- }
- catch ( SQLException err ) {
- System.out.println( err.getMessage( ) );
- }
- }
- public GuiApp1()
- {
- JFrame guiFrame = new JFrame();
- //make sure the program exits when the frame closes
- guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- guiFrame.setTitle("RPG Database Checker");
- guiFrame.setSize(500,500);
- //This will center the JFrame in the middle of the screen
- guiFrame.setLocationRelativeTo(null);
- //Options for the JComboBox
- String[] tableOptions = {"Characters", "Quests", "Items", "NPCs"};
- //The first JPanel contains a JLabel and JCombobox
- final JPanel comboPanel = new JPanel();
- final JPanel listPanel = new JPanel();
- final JPanel infoPanel = new JPanel();
- final JPanel allInfoPanel = new JPanel();
- JLabel comboLbl = new JLabel("Print a table:");
- JComboBox tables = new JComboBox(tableOptions);
- JTextArea textArea = new JTextArea(5, 5);
- JScrollPane scrollPane = new JScrollPane(textArea);
- JLabel listLbl = new JLabel("Contents:");
- JTextArea textArea2 = new JTextArea(20, 35);
- JScrollPane scrollPane2 = new JScrollPane(textArea2);
- JButton printAllButton = new JButton("This prints all info");
- final String newline = "\n";
- textArea.setEditable(false);
- textArea2.setEditable(false);
- //comboPanel
- comboPanel.add(comboLbl);
- comboPanel.add(tables);
- //infoPanel
- String text = "This will contain the info you chose";
- textArea.append(text + newline);
- infoPanel.add(textArea);
- //listPanel
- listPanel.add(listLbl);
- listPanel.setVisible(false);
- //allInfoPanel
- String text2 = "This will contain all table info";
- textArea2.append(text2 + newline);
- allInfoPanel.add(textArea2);
- allInfoPanel.setVisible(false);
- //The ActionListener class is used to handle the
- //event that happens when the user clicks the button.
- //As there is not a lot that needs to happen we can
- //define an anonymous inner class to make the code simpler.
- printAllButton.addActionListener(new ActionListener()
- {
- @Override
- public void actionPerformed(ActionEvent event)
- {
- //When the button is pressed
- //the setVisible value of the listPanel and
- //comboPanel is switched from true to
- //value or vice versa.
- String query = "SELECT *" + "FROM ´character´";
- String host = "jdbc:mysql://mysql.cc.puv.fi:3306/e1401175_projekti";
- String uName = "e1401175";
- String uPass= "jnCgtUXV7jZf";
- try {
- Connection con = DriverManager.getConnection(host, uName, uPass);
- try{
- PreparedStatement stm = con.prepareStatement(query);
- ResultSet rs = stm.executeQuery(query);
- StringBuilder strBuilder = new StringBuilder();
- while(rs.next()) {
- strBuilder.append(rs.getString(1)).append(" ").append(rs.getString(2));
- strBuilder.append("\n");
- }
- textArea.setText(strBuilder.toString());
- con.close();
- }
- catch(SQLException e) {
- System.out.println("Error in query!");
- }
- }
- catch ( SQLException err ) {
- System.out.println( err.getMessage( ) );
- }
- listPanel.setVisible(!listPanel.isVisible());
- infoPanel.setVisible(!infoPanel.isVisible());
- comboPanel.setVisible(!comboPanel.isVisible());
- allInfoPanel.setVisible(!allInfoPanel.isVisible());
- }
- }
- );
- //The JFrame uses the BorderLayout layout manager.
- //Put the two JPanels and JButton in different areas.
- guiFrame.add(comboPanel, BorderLayout.CENTER);
- guiFrame.add(infoPanel, BorderLayout.CENTER);
- guiFrame.add(listPanel, BorderLayout.NORTH);
- guiFrame.add(allInfoPanel, BorderLayout.CENTER);
- guiFrame.add(printAllButton,BorderLayout.SOUTH);
- //make sure the JFrame is visible
- guiFrame.setVisible(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement