Advertisement
Guest User

Untitled

a guest
May 2nd, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.00 KB | None | 0 0
  1. package start1;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Dimension;
  5. import java.awt.Font;
  6. import java.awt.Insets;
  7. import java.awt.event.ActionEvent;
  8. import java.awt.event.ActionListener;
  9. import java.io.IOException;
  10. import java.io.InputStream;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.ResultSetMetaData;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.util.Properties;
  18.  
  19. import javax.swing.BorderFactory;
  20. import javax.swing.JButton;
  21. import javax.swing.JComboBox;
  22. import javax.swing.JPanel;
  23. import javax.swing.JTextField;
  24.  
  25. @SuppressWarnings("serial")
  26. public class InputPanel extends JPanel {
  27.  
  28.     CommanderPanel superPanel;
  29.    
  30.     JComboBox dropDownList;
  31.     JTextField theTextField;
  32.     JButton executeButton;
  33.    
  34.     Connection theConnection = null;
  35.     Statement theStatement;
  36.    
  37.     public InputPanel(CommanderPanel thePanel) {
  38.        
  39.         superPanel = thePanel;
  40.        
  41.         String[] listItems = {
  42.                 "Create database <database name>",
  43.                 "List all databases",
  44.                 "Switch to database <database name>",
  45.                 "Show all tables in the current database",
  46.                 "Show fields in table <table name>",
  47.                 "Delete database <database name>",
  48.                 "Delete table <table name>",
  49.                 "Show all data in a table <table name>",
  50.                 "Show columns and column information of <table name>",
  51.                 "String Command",
  52.                 "String Query"
  53.                 }
  54.         ;
  55.        
  56.         dropDownList = new JComboBox(listItems);
  57.         dropDownList.setPreferredSize(new Dimension(100, 25));
  58.         dropDownList.setToolTipText("Select a command");
  59.         dropDownList.setSelectedIndex(10);
  60.        
  61.         theTextField = new JTextField();
  62.         theTextField.setMargin(new Insets(2, 2, 2, 2));
  63.         theTextField.setFont(superPanel.customFont.deriveFont(Font.BOLD, 14));
  64.         theTextField.setBorder(BorderFactory.createLoweredBevelBorder());
  65.         theTextField.setPreferredSize(new Dimension(100, 25));
  66.         theTextField.setText("SELECT * FROM jonruna_mystartpage;");
  67.         theTextField.setToolTipText("Command argument(s)");
  68.        
  69.         executeButton = new JButton("Execute");
  70.         executeButton.setPreferredSize(new Dimension(100, 25));
  71.         executeButton.setToolTipText("Execute the command");
  72.         ActionListener executeListener = new ActionListener() {
  73.             public void actionPerformed(ActionEvent e) {
  74.                 executeCommand();
  75.             }
  76.         };
  77.         executeButton.addActionListener(executeListener);
  78.        
  79.         BorderLayout theLayout = new BorderLayout();
  80.        
  81.         this.setLayout(theLayout);
  82.         this.add(dropDownList, BorderLayout.NORTH);
  83.         this.add(theTextField, BorderLayout.CENTER);
  84.         this.add(executeButton, BorderLayout.SOUTH);
  85.        
  86.         superPanel.theStatusPanel.outputServerErrorMsg("åäö ÅÄÖ");
  87.        
  88.     }
  89.    
  90.     public void TESTconnectToMySql() {
  91.         try {
  92.            
  93.             String url = "jdbc:mysql://localhost:3306/canvas?useUnicode=true&characterEncoding=iso-8859-1"; //?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8
  94.            
  95.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  96.            
  97.             Properties prop = new Properties();
  98.             //prop.put("socketFactory", "com.mysql.jdbc.NamedPipeSocketFactory");
  99.             //prop.put("charSet", "UTF-8");
  100.             //prop.put("characterEncoding", "UTF-8");
  101.             //prop.put("useUnicode", "true");
  102.             //prop.put("useUnicode", "true");
  103.             //prop.put("charSet", "UTF-8");
  104.             //prop.put("characterEncoding", "UTF-8");
  105.            
  106.             prop.put("user", "root");
  107.             prop.put("password", "propan11");
  108.            
  109.             theConnection = DriverManager.getConnection(url, prop);
  110.            
  111.             if(!theConnection.isClosed()) {
  112.                 outputServerMsg("Successfully connected to MySQL server...");
  113.             }
  114.            
  115.             theStatement = theConnection.createStatement();
  116.            
  117.            
  118.             //stmt.executeUpdate(STRING) d�r STRING �r ett kommando att executa. den returnar en info string ocks�.
  119.             //System.out.println(stmt.executeUpdate("CREATE DATABASE JunkDB2"));    //creata en databas
  120.             //System.out.println(theStatement.executeUpdate("DROP DATABASE JunkDB2"));      //deleta en databas
  121.            
  122.         } catch(Exception e) {
  123.             outputServerErrorMsg("Exception: " + e.getMessage());
  124.         }
  125.     }
  126.    
  127.     public void connectToMySql() {
  128.         try {
  129.            
  130.             String url = "jdbc:mysql://localhost:3306/canvas?useUnicode=true&characterEncoding=UTF-8"; //?useUnicode=true&characterEncoding=UTF-8&charSet=UTF-8
  131.            
  132.             Class.forName("com.mysql.jdbc.Driver").newInstance();
  133.            
  134.             Properties prop = new Properties();
  135.             //prop.put("socketFactory", "com.mysql.jdbc.NamedPipeSocketFactory");
  136.             //prop.put("charSet", "UTF-8");
  137.             //prop.put("characterEncoding", "UTF-8");
  138.             //prop.put("useUnicode", "true");
  139.             prop.put("user", "root");
  140.             prop.put("password", "propan11");
  141.            
  142.             theConnection = DriverManager.getConnection(url, prop);
  143.            
  144.             if(!theConnection.isClosed()) {
  145.                 outputServerMsg("Successfully connected to MySQL server...");
  146.             }
  147.            
  148.             theStatement = theConnection.createStatement();
  149.            
  150.             //stmt.executeUpdate(STRING) där STRING är ett kommando att executa. den returnar en info string också.
  151.             //System.out.println(stmt.executeUpdate("CREATE DATABASE JunkDB2"));    //creata en databas
  152.             //System.out.println(theStatement.executeUpdate("DROP DATABASE JunkDB2"));      //deleta en databas
  153.            
  154.         } catch(Exception e) {
  155.             outputServerErrorMsg("Exception: " + e.getMessage());
  156.         }
  157.     }
  158.    
  159.     public void outputCommandMsg(String theString) {
  160.         superPanel.theStatusPanel.outputCommandMsg(theString);
  161.     }
  162.    
  163.     public void outputServerMsg(String theString) {
  164.         superPanel.theStatusPanel.outputServerMsg(theString);
  165.     }
  166.    
  167.     public void outputServerErrorMsg(String theString) {
  168.         superPanel.theStatusPanel.outputServerErrorMsg(theString);
  169.     }
  170.    
  171.     public void executeCommand() {
  172.        
  173.         int commandIndex = dropDownList.getSelectedIndex();
  174.        
  175.         //outputCommandMsg("Selected item has index: " + commandIndex);
  176.         //outputServerMsg("Server response");
  177.         //outputServerErrorMsg("Server error response");
  178.        
  179.         /*
  180.                 "Create database <database name>",
  181.                 "List all databases",
  182.                 "Switch to database <database name>",
  183.                 "Show all tables in the current database",
  184.                 "Show fields in table <table name>",
  185.                 "Delete database <database name>",
  186.                 "Delete table <table name>",
  187.                 "Show all data in a table <table name>",
  188.                 "Show columns and column information of <table name>"
  189.         */
  190.         switch (commandIndex) {
  191.        
  192.         case 0: //"Create database <database name>"
  193.             createDataBase(theTextField.getText());
  194.             break;
  195.        
  196.         case 1: //"List all databases"
  197.             listDataBases();
  198.             break;
  199.        
  200.         case 2: //"Switch to database <database name>"
  201.            
  202.             break;
  203.        
  204.         case 3: //"Show all tables in the current database"
  205.            
  206.             break;
  207.        
  208.         case 4: //"Show fields in table <table name>"
  209.            
  210.             break;
  211.        
  212.         case 5: //"Delete database <database name>"
  213.             deleteDataBase(theTextField.getText());
  214.             break;
  215.        
  216.         case 6: //"Delete table <table name>"
  217.            
  218.             break;
  219.        
  220.         case 7: //"Show all data in a table <table name>"
  221.            
  222.             break;
  223.        
  224.         case 8: //"Show columns and column information of <table name>"
  225.            
  226.             break;
  227.        
  228.         case 9: // String command
  229.             mySqlCommand(theTextField.getText());
  230.             break;
  231.        
  232.         case 10: // String command
  233.             mySqlQuery(theTextField.getText());
  234.             break;
  235.        
  236.         }
  237.        
  238.         //TODO outputStatMsg med commandot som skickas till MySQL servern, outputServerMsg med det server ger som response.
  239.        
  240.     }
  241.    
  242.     public void mySqlQuery(String commandString) { //returna ett v�rde fr�n servern.
  243.        
  244.         outputCommandMsg(commandString);
  245.        
  246.         try {
  247.             ResultSet rs = theStatement.executeQuery(commandString);
  248.            
  249.             ResultSetMetaData rsMetaData = rs.getMetaData();
  250.            
  251.             int numberOfColumns = rsMetaData.getColumnCount();
  252.            
  253.  
  254.             while (rs.next()){
  255.                
  256.                 for (int i = 1; i <= numberOfColumns; i++) {
  257.                    
  258.                     String outputString = "";
  259.                     InputStream stream = rs.getBinaryStream(i);
  260.                     int theChar;
  261.                    
  262.                     try {
  263.                         theChar = stream.read();
  264.                         while (theChar != -1) {
  265.                             outputString += (char)theChar;
  266.                             theChar = stream.read();
  267.                         }
  268.                        
  269.                     } catch (IOException e) {
  270.                         // TODO Auto-generated catch block
  271.                         e.printStackTrace();
  272.                     }
  273.                    
  274.                     outputServerMsg(outputString);
  275.                     System.out.println(outputString);
  276.                    
  277.                 }
  278.                
  279.             }
  280.            
  281.         } catch (SQLException e) {
  282.             outputServerErrorMsg(e.getMessage());
  283.         }
  284.        
  285.         //outputServerMsg("If there is a succesful server response this method should output it");
  286.         //outputServerErrorMsg("If there is a server error this method should output it");
  287.        
  288.     }
  289.    
  290.     public void mySqlCommand(String commandString) { //skicka commandot till server.
  291.        
  292.         outputCommandMsg(commandString);
  293.        
  294.         try {
  295.             theStatement.executeUpdate(commandString);
  296.            
  297.             //outputServerMsg("" + theStatement.executeUpdate(commandString));
  298.            
  299.            
  300.         } catch (SQLException e) {
  301.             outputServerErrorMsg(e.getMessage());
  302.         }
  303.        
  304.     }
  305.    
  306.     public void createDataBase(String databaseName) {
  307.        
  308.         mySqlCommand("create database " + databaseName + ";");
  309.        
  310.     }
  311.    
  312.     public void listDataBases() {
  313.        
  314.         mySqlQuery("show databases;");
  315.        
  316.     }
  317.    
  318.     public void deleteDataBase(String databaseName) {
  319.        
  320.         mySqlCommand("drop database " + databaseName + ";");
  321.        
  322.     }
  323.    
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement