Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //not the best but a good start off
- package suggestionform;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.*;
- public class SuggestionForm extends JFrame {
- JLabel idLB, nameLB, emailLB, suggLB;
- JTextField idTF, nameTF, emailTF;
- JTextArea suggTA;
- JButton saveBtn, cancelBtn;
- //CONSTRUCTOR
- public SuggestionForm() {
- setTitle("Suggestion Form");
- setSize(500, 300);
- setVisible(true);
- setDefaultCloseOperation(EXIT_ON_CLOSE);
- //DEFINE CONTAINER
- Container form = getContentPane();
- //DEFINE LAYOUT
- form.setLayout(new GridLayout(5, 2));
- //CREATE COMPONENTS
- idLB = new JLabel("Student ID");
- idTF = new JTextField();
- nameLB = new JLabel("Name");
- nameTF = new JTextField();
- emailLB = new JLabel("Email Address");
- emailTF = new JTextField();
- suggLB = new JLabel("Suggestion");
- suggTA = new JTextArea();
- saveBtn = new JButton("Save");
- cancelBtn = new JButton("Cancel");
- //ADD COMPONENTS TO CONTAINER
- form.add(idLB);
- form.add(idTF);
- form.add(nameLB);
- form.add(nameTF);
- form.add(emailLB);
- form.add(emailTF);
- form.add(suggLB);
- form.add(suggTA);
- form.add(saveBtn);
- form.add(cancelBtn);
- //Save BTN
- Save save = new Save();
- saveBtn.addActionListener(save);
- //Cancel BTN
- Cancel cancel = new Cancel();
- cancelBtn.addActionListener(cancel);
- }
- private class Save implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent ae) {
- String id, name, email, suggestion;
- id = idTF.getText();
- name = nameTF.getText();
- email = emailTF.getText();
- suggestion = suggTA.getText();
- //SAVE TO DB
- //create connection
- String db_connect ="jdbc:mysql://localhost:3307/School";
- String username = "root";
- String password ="";
- //insert into database
- String query = "insert into Class (id,name,email,suggestion)values('" + id + "','" + name + "','" + email + "','" + suggestion + "')";
- try {
- //get connection
- Class.forName("com.mysql.jdbc.Driver");
- Connection con = DriverManager.getConnection(db_connect,username,password);
- Statement stmt = con.prepareStatement(query);
- stmt.execute(query);
- }
- catch (ClassNotFoundException ex) {
- ex.printStackTrace();
- } catch (SQLException ex) {
- Logger.getLogger(SuggestionForm.class.getName()).log(Level.SEVERE, null, ex);
- }
- }
- }
- private class Cancel implements ActionListener {
- @Override
- public void actionPerformed(ActionEvent ae) {
- idTF.setText("");
- nameTF.setText("");
- emailTF.setText("");
- suggTA.setText("");
- }
- }
- public static void main(String[] args) {
- SuggestionForm form = new SuggestionForm();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement