Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- public class Students extends JFrame implements ActionListener
- {
- JLabel l_name, l_year, l_major, l_ft, l_email; // declaring Lebels for all inputs
- JTextField name, major, email; //declaring text field for input data
- String[] choice = {"Yes","No"};
- String[] arrayYear = {"1","2","3","4"};
- JComboBox FullTime = new JComboBox(choice); //Declaring and creating combo box to select among yes or no
- JComboBox year = new JComboBox(arrayYear); //Declaring and creating Combo box to select year
- JButton Insert, Clear; //declaring button for insert and clear
- Students()
- {
- setVisible(true);
- setSize(700, 700);
- setLayout(null);
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- setTitle("Students"); //set the title of the frame
- l_name = new JLabel("Name:"); //adding text the lable
- l_year = new JLabel("Year:");//adding text the lable
- l_major = new JLabel("Major:");//adding text the lable
- l_ft = new JLabel("FullTime:");//adding text the lable
- l_email = new JLabel("Email:");//adding text the lable
- name = new JTextField(); //creating text field to input data
- major = new JTextField(); //creating text field to input data
- email = new JTextField(); //creating text field to input data
- Insert = new JButton("Insert"); //creating Button
- Clear = new JButton("Clear"); //creating Button
- Insert.addActionListener(this); //adding event listner to button "Insert"
- Clear.addActionListener(this); //adding event listner to button "Clear"
- add(l_name); //adding the element to frame
- add(name);//adding the element to frame
- add(l_year);//adding the element to frame
- add(year);//adding the element to frame
- add(l_major);//adding the element to frame
- add(major);//adding the element to frame
- add(l_ft);//adding the element to frame
- add(FullTime);//adding the element to frame
- add(l_email);//adding the element to frame
- add(email);//adding the element to frame
- add(Insert);//adding the element to frame
- add(Clear);//adding the element to frame
- }
- public void actionPerformed(ActionEvent e)
- {
- if (e.getSource() == Insert) //If event is "Insert" i.e Insert button is clicked
- {
- String s1="jdbc:ucanaccess://";
- String user="";
- String pass="";
- String s2="students.accdb";
- String URL = s1+s2;
- ResultSet result;
- result = connectToDB(URL); printDB(result);
- try
- {
- String database="classes.mdb";//Databse with full path. In this case database exists in the current directory
- String url=("jdbc:ucanaccess:///Users/sherifatu.sumaila/mydb.mdb");
- Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
- Connection con=DriverManager.getConnection("jdbc:ucanaccess://Students.accdb");
- Statement st=con.createStatement();
- String sql = ("Insert Into StudentData (name,year,major,fullTime,email)" +
- "Values ('" +
- name.getText() + "','" + year.getSelectedIndex() + "','" + major.getText() + "','" +
- fullTime.getSelectedIndex() + "','" + email.getText() + "')";
- Statement statement = con.createStatement();
- statement.execute(sql);
- display("Student " + name.getText()+ " inserted successfully");
- clear();
- }
- catch(Exception e) {
- System.out.println(e.getMessage());
- }
- }
- else
- {
- name.setText("");//clearing the field
- major.setText("");//clearing the field
- email.setText("");//clearing the field
- }
- }
- public void display(String msg) {
- JOptionPane.showMessageDialog(null, msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement