Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package connectToMySQL;
- import java.awt.EventQueue;
- import java.sql.Statement;
- import javax.swing.JFrame;
- import javax.swing.JTextField;
- import java.awt.BorderLayout;
- import javax.swing.JButton;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.awt.event.ActionEvent;
- import javax.swing.JEditorPane;
- import javax.swing.JTextPane;
- import com.mysql.cj.jdbc.result.ResultSetMetaData;
- import com.mysql.cj.protocol.Resultset;
- import javax.swing.JLabel;
- import javax.swing.JTable;
- public class gui {
- private JFrame frame;
- private JButton login;
- private JButton logout;
- private JButton query;
- private JButton execute;
- private JTextField addr;
- private JTextField port;
- private JEditorPane sqlSent;
- private JTextPane notificationPanel;
- private JLabel lblNewLabel;
- private JLabel lblNewLabel_1;
- private JLabel lblNewLabel_2;
- private JTextPane textPane;
- public String address;
- public String porta;
- public String user;
- public String passw;
- public String q;
- public ResultSet select;
- Connection conn;
- Statement st;
- String sentence;
- ResultSet rs;
- private JTextField usr;
- private JTextField pass;
- private JTextPane output;
- private JLabel status;
- private JLabel cause;
- /**
- * Launch the application.
- */
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- try {
- gui window = new gui();
- window.frame.setVisible(true);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
- }
- /**
- * Create the application.
- */
- public gui() {
- initialize();
- }
- /**
- * Initialize the contents of the frame.
- */
- private void initialize() {
- frame = new JFrame();
- frame.setBounds(100, 100, 632, 461);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.getContentPane().setLayout(null);
- frame.getContentPane().add(getLogin());
- frame.getContentPane().add(getLogout());
- frame.getContentPane().add(getQuery());
- frame.getContentPane().add(getExecute());
- frame.getContentPane().add(getAddr());
- frame.getContentPane().add(getPort());
- frame.getContentPane().add(getSqlSent());
- frame.getContentPane().add(getNotificationPanel());
- frame.getContentPane().add(getLblNewLabel());
- frame.getContentPane().add(getLblNewLabel_1());
- frame.getContentPane().add(getLblNewLabel_2());
- frame.getContentPane().add(getTextPane());
- frame.getContentPane().add(getUsr());
- frame.getContentPane().add(getPass());
- frame.getContentPane().add(getOutput());
- frame.getContentPane().add(getStatus());
- frame.getContentPane().add(getCause());
- }
- private JButton getLogin() {
- if (login == null) {
- login = new JButton("Login");
- login.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- address = addr.getText();
- porta = port.getText();
- user = usr.getText();
- passw = pass.getText();
- System.out.printf("Connetto %s : %s \n", address, porta);
- try {
- conn = DriverManager.getConnection(
- "jdbc:mysql://"+address+":"+porta,
- user, passw);
- conn.setAutoCommit(true);
- st = conn.createStatement();
- notificationPanel.setText("CONNECTED to " + address + " identified with " + user);
- System.out.println("Conected!");
- } catch (SQLException eee) {
- eee.printStackTrace();
- }
- }
- });
- login.setBounds(290, 6, 117, 29);
- }
- return login;
- }
- private JButton getLogout() {
- if (logout == null) {
- logout = new JButton("Logout");
- logout.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- try {
- conn.close();
- st.close();
- notificationPanel.setText("DISCONNECTED from " + address + " with " + user);
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- }
- }
- });
- logout.setBounds(402, 6, 117, 29);
- }
- return logout;
- }
- private JButton getQuery() {
- if (query == null) {
- query = new JButton("Query");
- query.setBounds(290, 81, 117, 29);
- query.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- q = sqlSent.getText();
- String otp = "";
- try {
- select = st.executeQuery(q);
- ResultSetMetaData rsmd = (ResultSetMetaData) select.getMetaData();
- int columnsNumber = rsmd.getColumnCount();
- int x = 0;
- while (select.next()) {
- for (int i = 1; i <= columnsNumber; i++) {
- if (i == 1) {
- otp = otp + "\n";
- x++;
- }
- if (i > 1) otp = otp + ", ";
- String columnValue = select.getString(i);
- otp = otp + rsmd.getColumnName(i) + " " + columnValue;
- }
- output.setText(otp);
- status.setText("OK");
- notificationPanel.setText("Successfull selected " + x + "row(s)");
- }
- } catch (SQLException e1) {
- // TODO Auto-generated catch block
- status.setText("FAILURE");
- e1.printStackTrace();
- notificationPanel.setText(e1.getMessage());
- String sentence = e1.getMessage();
- String syntax = "syntax";
- String permission = "denied";
- String procedure = "procedure";
- if ( sentence.toLowerCase().indexOf(syntax.toLowerCase()) != -1 ) {
- cause.setText("Syntax Error");
- }else if ( sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1 ) {
- cause.setText("Permission Denied");
- }else if (sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1) {
- cause.setText("Procedure Error");
- } else {
- System.out.println("ERROR");
- }
- }
- }
- });
- }
- return query;
- }
- private JButton getExecute() {
- if (execute == null) {
- execute = new JButton("Execute");
- execute.addActionListener(new ActionListener() {
- public void actionPerformed(ActionEvent e) {
- String exe;
- int x;
- exe = sqlSent.getText();
- try {
- x = st.executeUpdate(exe);
- notificationPanel.setText("Ok, " + x + "row(s) affected");
- status.setText("OK");
- } catch (SQLException e1) {
- status.setText("FAILURE");
- e1.printStackTrace();
- notificationPanel.setText(e1.getMessage());
- String sentence = e1.getMessage();
- String syntax = "syntax";
- String permission = "denied";
- String procedure = "procedure";
- if ( sentence.toLowerCase().indexOf(syntax.toLowerCase()) != -1 ) {
- cause.setText("Syntax Error");
- }else if ( sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1 ) {
- cause.setText("Permission Denied");
- }else if (sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1) {
- cause.setText("Procedure Error");
- } else {
- System.out.println("ERROR");
- }
- }
- }
- });
- execute.setBounds(290, 111, 117, 29);
- }
- return execute;
- }
- private JTextField getAddr() {
- if (addr == null) {
- addr = new JTextField();
- addr.setText("serer address");
- addr.setBounds(16, 6, 130, 26);
- addr.setColumns(10);
- }
- return addr;
- }
- private JTextField getPort() {
- if (port == null) {
- port = new JTextField();
- port.setText("port");
- port.setBounds(153, 6, 60, 26);
- port.setColumns(10);
- }
- return port;
- }
- private JEditorPane getSqlSent() {
- if (sqlSent == null) {
- sqlSent = new JEditorPane();
- sqlSent.setText("SQL sentence");
- sqlSent.setBounds(16, 47, 260, 93);
- }
- return sqlSent;
- }
- private JTextPane getNotificationPanel() {
- if (notificationPanel == null) {
- notificationPanel = new JTextPane();
- notificationPanel.setEditable(false);
- notificationPanel.setText("Notifications");
- notificationPanel.setBounds(16, 288, 301, 58);
- }
- return notificationPanel;
- }
- private JLabel getLblNewLabel() {
- if (lblNewLabel == null) {
- lblNewLabel = new JLabel("Status:");
- lblNewLabel.setBounds(16, 358, 61, 16);
- }
- return lblNewLabel;
- }
- private JLabel getLblNewLabel_1() {
- if (lblNewLabel_1 == null) {
- lblNewLabel_1 = new JLabel("Cause:");
- lblNewLabel_1.setBounds(276, 358, 61, 16);
- }
- return lblNewLabel_1;
- }
- private JLabel getLblNewLabel_2() {
- if (lblNewLabel_2 == null) {
- lblNewLabel_2 = new JLabel("Missing permissions:");
- lblNewLabel_2.setBounds(184, 386, 166, 16);
- }
- return lblNewLabel_2;
- }
- private JTextPane getTextPane() {
- if (textPane == null) {
- textPane = new JTextPane();
- textPane.setEditable(false);
- textPane.setBounds(362, 385, 157, 48);
- }
- return textPane;
- }
- private JTextField getUsr() {
- if (usr == null) {
- usr = new JTextField();
- usr.setText("User");
- usr.setBounds(300, 43, 97, 26);
- usr.setColumns(10);
- }
- return usr;
- }
- private JTextField getPass() {
- if (pass == null) {
- pass = new JTextField();
- pass.setText("Password");
- pass.setBounds(402, 43, 107, 26);
- pass.setColumns(10);
- }
- return pass;
- }
- private JTextPane getOutput() {
- if (output == null) {
- output = new JTextPane();
- output.setEditable(false);
- output.setBounds(16, 152, 594, 127);
- }
- return output;
- }
- private JLabel getStatus() {
- if (status == null) {
- status = new JLabel("- - -");
- status.setBounds(65, 358, 61, 16);
- }
- return status;
- }
- private JLabel getCause() {
- if (cause == null) {
- cause = new JLabel("");
- cause.setBounds(361, 358, 90, 16);
- }
- return cause;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement