Advertisement
Guest User

Untitled

a guest
Mar 1st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. //not the best but a good start off
  2. package suggestionform;
  3. import java.awt.*;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.SQLException;
  9. import java.sql.Statement;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.*;
  13.  
  14. public class SuggestionForm extends JFrame {
  15.  
  16. JLabel idLB, nameLB, emailLB, suggLB;
  17. JTextField idTF, nameTF, emailTF;
  18. JTextArea suggTA;
  19. JButton saveBtn, cancelBtn;
  20.  
  21. //CONSTRUCTOR
  22. public SuggestionForm() {
  23. setTitle("Suggestion Form");
  24. setSize(500, 300);
  25. setVisible(true);
  26. setDefaultCloseOperation(EXIT_ON_CLOSE);
  27.  
  28. //DEFINE CONTAINER
  29. Container form = getContentPane();
  30.  
  31. //DEFINE LAYOUT
  32. form.setLayout(new GridLayout(5, 2));
  33.  
  34. //CREATE COMPONENTS
  35. idLB = new JLabel("Student ID");
  36. idTF = new JTextField();
  37. nameLB = new JLabel("Name");
  38. nameTF = new JTextField();
  39. emailLB = new JLabel("Email Address");
  40. emailTF = new JTextField();
  41. suggLB = new JLabel("Suggestion");
  42. suggTA = new JTextArea();
  43. saveBtn = new JButton("Save");
  44. cancelBtn = new JButton("Cancel");
  45.  
  46. //ADD COMPONENTS TO CONTAINER
  47. form.add(idLB);
  48. form.add(idTF);
  49. form.add(nameLB);
  50. form.add(nameTF);
  51. form.add(emailLB);
  52. form.add(emailTF);
  53. form.add(suggLB);
  54. form.add(suggTA);
  55. form.add(saveBtn);
  56. form.add(cancelBtn);
  57.  
  58. //Save BTN
  59. Save save = new Save();
  60. saveBtn.addActionListener(save);
  61. //Cancel BTN
  62. Cancel cancel = new Cancel();
  63. cancelBtn.addActionListener(cancel);
  64.  
  65. }
  66.  
  67. private class Save implements ActionListener {
  68.  
  69. @Override
  70. public void actionPerformed(ActionEvent ae) {
  71. String id, name, email, suggestion;
  72. id = idTF.getText();
  73. name = nameTF.getText();
  74. email = emailTF.getText();
  75. suggestion = suggTA.getText();
  76.  
  77.  
  78. //SAVE TO DB
  79. //create connection
  80. String db_connect ="jdbc:mysql://localhost:3307/School";
  81. String username = "root";
  82. String password ="";
  83. //insert into database
  84. String query = "insert into Class (id,name,email,suggestion)values('" + id + "','" + name + "','" + email + "','" + suggestion + "')";
  85.  
  86. try {
  87. //get connection
  88. Class.forName("com.mysql.jdbc.Driver");
  89. Connection con = DriverManager.getConnection(db_connect,username,password);
  90. Statement stmt = con.prepareStatement(query);
  91. stmt.execute(query);
  92. }
  93. catch (ClassNotFoundException ex) {
  94. ex.printStackTrace();
  95. } catch (SQLException ex) {
  96. Logger.getLogger(SuggestionForm.class.getName()).log(Level.SEVERE, null, ex);
  97. }
  98.  
  99. }
  100. }
  101. private class Cancel implements ActionListener {
  102.  
  103. @Override
  104. public void actionPerformed(ActionEvent ae) {
  105. idTF.setText("");
  106. nameTF.setText("");
  107. emailTF.setText("");
  108. suggTA.setText("");
  109. }
  110. }
  111.  
  112.  
  113. public static void main(String[] args) {
  114. SuggestionForm form = new SuggestionForm();
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement