Advertisement
Guest User

Untitled

a guest
Feb 14th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.57 KB | None | 0 0
  1. package connectToMySQL;
  2.  
  3. import java.awt.EventQueue;
  4. import java.sql.Statement;
  5.  
  6. import javax.swing.JFrame;
  7. import javax.swing.JTextField;
  8. import java.awt.BorderLayout;
  9. import javax.swing.JButton;
  10. import java.awt.event.ActionListener;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.awt.event.ActionEvent;
  16. import javax.swing.JEditorPane;
  17. import javax.swing.JTextPane;
  18.  
  19. import com.mysql.cj.jdbc.result.ResultSetMetaData;
  20. import com.mysql.cj.protocol.Resultset;
  21.  
  22. import javax.swing.JLabel;
  23. import javax.swing.JTable;
  24.  
  25. public class gui {
  26.  
  27. private JFrame frame;
  28. private JButton login;
  29. private JButton logout;
  30. private JButton query;
  31. private JButton execute;
  32. private JTextField addr;
  33. private JTextField port;
  34. private JEditorPane sqlSent;
  35. private JTextPane notificationPanel;
  36. private JLabel lblNewLabel;
  37. private JLabel lblNewLabel_1;
  38. private JLabel lblNewLabel_2;
  39. private JTextPane textPane;
  40.  
  41. public String address;
  42. public String porta;
  43. public String user;
  44. public String passw;
  45. public String q;
  46. public ResultSet select;
  47.  
  48. Connection conn;
  49. Statement st;
  50. String sentence;
  51. ResultSet rs;
  52. private JTextField usr;
  53. private JTextField pass;
  54. private JTextPane output;
  55. private JLabel status;
  56. private JLabel cause;
  57.  
  58. /**
  59. * Launch the application.
  60. */
  61. public static void main(String[] args) {
  62. EventQueue.invokeLater(new Runnable() {
  63. public void run() {
  64. try {
  65. gui window = new gui();
  66. window.frame.setVisible(true);
  67. } catch (Exception e) {
  68. e.printStackTrace();
  69. }
  70. }
  71. });
  72. }
  73.  
  74. /**
  75. * Create the application.
  76. */
  77. public gui() {
  78. initialize();
  79. }
  80.  
  81. /**
  82. * Initialize the contents of the frame.
  83. */
  84. private void initialize() {
  85. frame = new JFrame();
  86. frame.setBounds(100, 100, 632, 461);
  87. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  88. frame.getContentPane().setLayout(null);
  89. frame.getContentPane().add(getLogin());
  90. frame.getContentPane().add(getLogout());
  91. frame.getContentPane().add(getQuery());
  92. frame.getContentPane().add(getExecute());
  93. frame.getContentPane().add(getAddr());
  94. frame.getContentPane().add(getPort());
  95. frame.getContentPane().add(getSqlSent());
  96. frame.getContentPane().add(getNotificationPanel());
  97. frame.getContentPane().add(getLblNewLabel());
  98. frame.getContentPane().add(getLblNewLabel_1());
  99. frame.getContentPane().add(getLblNewLabel_2());
  100. frame.getContentPane().add(getTextPane());
  101. frame.getContentPane().add(getUsr());
  102. frame.getContentPane().add(getPass());
  103. frame.getContentPane().add(getOutput());
  104. frame.getContentPane().add(getStatus());
  105. frame.getContentPane().add(getCause());
  106. }
  107. private JButton getLogin() {
  108. if (login == null) {
  109. login = new JButton("Login");
  110. login.addActionListener(new ActionListener() {
  111. public void actionPerformed(ActionEvent e) {
  112. address = addr.getText();
  113. porta = port.getText();
  114. user = usr.getText();
  115. passw = pass.getText();
  116. System.out.printf("Connetto %s : %s \n", address, porta);
  117. try {
  118. conn = DriverManager.getConnection(
  119. "jdbc:mysql://"+address+":"+porta,
  120. user, passw);
  121. conn.setAutoCommit(true);
  122. st = conn.createStatement();
  123. notificationPanel.setText("CONNECTED to " + address + " identified with " + user);
  124. System.out.println("Conected!");
  125. } catch (SQLException eee) {
  126. eee.printStackTrace();
  127. }
  128. }
  129. });
  130. login.setBounds(290, 6, 117, 29);
  131. }
  132. return login;
  133. }
  134. private JButton getLogout() {
  135. if (logout == null) {
  136. logout = new JButton("Logout");
  137. logout.addActionListener(new ActionListener() {
  138. public void actionPerformed(ActionEvent e) {
  139. try {
  140. conn.close();
  141. st.close();
  142. notificationPanel.setText("DISCONNECTED from " + address + " with " + user);
  143. } catch (SQLException e1) {
  144. // TODO Auto-generated catch block
  145. e1.printStackTrace();
  146. }
  147.  
  148. }
  149. });
  150. logout.setBounds(402, 6, 117, 29);
  151.  
  152. }
  153. return logout;
  154. }
  155. private JButton getQuery() {
  156. if (query == null) {
  157. query = new JButton("Query");
  158. query.setBounds(290, 81, 117, 29);
  159.  
  160. query.addActionListener(new ActionListener() {
  161. public void actionPerformed(ActionEvent e) {
  162. q = sqlSent.getText();
  163. String otp = "";
  164. try {
  165. select = st.executeQuery(q);
  166. ResultSetMetaData rsmd = (ResultSetMetaData) select.getMetaData();
  167. int columnsNumber = rsmd.getColumnCount();
  168. int x = 0;
  169. while (select.next()) {
  170. for (int i = 1; i <= columnsNumber; i++) {
  171. if (i == 1) {
  172. otp = otp + "\n";
  173. x++;
  174. }
  175. if (i > 1) otp = otp + ", ";
  176. String columnValue = select.getString(i);
  177. otp = otp + rsmd.getColumnName(i) + " " + columnValue;
  178. }
  179. output.setText(otp);
  180. status.setText("OK");
  181. notificationPanel.setText("Successfull selected " + x + "row(s)");
  182. }
  183. } catch (SQLException e1) {
  184. // TODO Auto-generated catch block
  185. status.setText("FAILURE");
  186. e1.printStackTrace();
  187. notificationPanel.setText(e1.getMessage());
  188. String sentence = e1.getMessage();
  189. String syntax = "syntax";
  190. String permission = "denied";
  191. String procedure = "procedure";
  192.  
  193. if ( sentence.toLowerCase().indexOf(syntax.toLowerCase()) != -1 ) {
  194. cause.setText("Syntax Error");
  195. }else if ( sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1 ) {
  196. cause.setText("Permission Denied");
  197. }else if (sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1) {
  198. cause.setText("Procedure Error");
  199. } else {
  200.  
  201. System.out.println("ERROR");
  202.  
  203. }
  204. }
  205. }
  206. });
  207.  
  208. }
  209. return query;
  210. }
  211. private JButton getExecute() {
  212. if (execute == null) {
  213. execute = new JButton("Execute");
  214. execute.addActionListener(new ActionListener() {
  215. public void actionPerformed(ActionEvent e) {
  216. String exe;
  217. int x;
  218. exe = sqlSent.getText();
  219. try {
  220. x = st.executeUpdate(exe);
  221. notificationPanel.setText("Ok, " + x + "row(s) affected");
  222. status.setText("OK");
  223. } catch (SQLException e1) {
  224. status.setText("FAILURE");
  225. e1.printStackTrace();
  226. notificationPanel.setText(e1.getMessage());
  227. String sentence = e1.getMessage();
  228. String syntax = "syntax";
  229. String permission = "denied";
  230. String procedure = "procedure";
  231.  
  232. if ( sentence.toLowerCase().indexOf(syntax.toLowerCase()) != -1 ) {
  233. cause.setText("Syntax Error");
  234. }else if ( sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1 ) {
  235. cause.setText("Permission Denied");
  236. }else if (sentence.toLowerCase().indexOf(permission.toLowerCase()) != -1) {
  237. cause.setText("Procedure Error");
  238. } else {
  239.  
  240. System.out.println("ERROR");
  241.  
  242. }
  243. }
  244.  
  245. }
  246. });
  247. execute.setBounds(290, 111, 117, 29);
  248. }
  249. return execute;
  250. }
  251. private JTextField getAddr() {
  252. if (addr == null) {
  253. addr = new JTextField();
  254. addr.setText("serer address");
  255. addr.setBounds(16, 6, 130, 26);
  256. addr.setColumns(10);
  257. }
  258. return addr;
  259. }
  260. private JTextField getPort() {
  261. if (port == null) {
  262. port = new JTextField();
  263. port.setText("port");
  264. port.setBounds(153, 6, 60, 26);
  265. port.setColumns(10);
  266. }
  267. return port;
  268. }
  269. private JEditorPane getSqlSent() {
  270. if (sqlSent == null) {
  271. sqlSent = new JEditorPane();
  272. sqlSent.setText("SQL sentence");
  273. sqlSent.setBounds(16, 47, 260, 93);
  274. }
  275. return sqlSent;
  276. }
  277. private JTextPane getNotificationPanel() {
  278. if (notificationPanel == null) {
  279. notificationPanel = new JTextPane();
  280. notificationPanel.setEditable(false);
  281. notificationPanel.setText("Notifications");
  282. notificationPanel.setBounds(16, 288, 301, 58);
  283. }
  284. return notificationPanel;
  285. }
  286. private JLabel getLblNewLabel() {
  287. if (lblNewLabel == null) {
  288. lblNewLabel = new JLabel("Status:");
  289. lblNewLabel.setBounds(16, 358, 61, 16);
  290. }
  291. return lblNewLabel;
  292. }
  293. private JLabel getLblNewLabel_1() {
  294. if (lblNewLabel_1 == null) {
  295. lblNewLabel_1 = new JLabel("Cause:");
  296. lblNewLabel_1.setBounds(276, 358, 61, 16);
  297. }
  298. return lblNewLabel_1;
  299. }
  300. private JLabel getLblNewLabel_2() {
  301. if (lblNewLabel_2 == null) {
  302. lblNewLabel_2 = new JLabel("Missing permissions:");
  303. lblNewLabel_2.setBounds(184, 386, 166, 16);
  304. }
  305. return lblNewLabel_2;
  306. }
  307. private JTextPane getTextPane() {
  308. if (textPane == null) {
  309. textPane = new JTextPane();
  310. textPane.setEditable(false);
  311. textPane.setBounds(362, 385, 157, 48);
  312. }
  313. return textPane;
  314. }
  315. private JTextField getUsr() {
  316. if (usr == null) {
  317. usr = new JTextField();
  318. usr.setText("User");
  319. usr.setBounds(300, 43, 97, 26);
  320. usr.setColumns(10);
  321. }
  322. return usr;
  323. }
  324. private JTextField getPass() {
  325. if (pass == null) {
  326. pass = new JTextField();
  327. pass.setText("Password");
  328. pass.setBounds(402, 43, 107, 26);
  329. pass.setColumns(10);
  330. }
  331. return pass;
  332. }
  333. private JTextPane getOutput() {
  334. if (output == null) {
  335. output = new JTextPane();
  336. output.setEditable(false);
  337. output.setBounds(16, 152, 594, 127);
  338. }
  339. return output;
  340. }
  341. private JLabel getStatus() {
  342. if (status == null) {
  343. status = new JLabel("- - -");
  344. status.setBounds(65, 358, 61, 16);
  345. }
  346. return status;
  347. }
  348. private JLabel getCause() {
  349. if (cause == null) {
  350. cause = new JLabel("");
  351. cause.setBounds(361, 358, 90, 16);
  352. }
  353. return cause;
  354. }
  355. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement