Advertisement
Guest User

Untitled

a guest
Mar 12th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.61 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import java.sql.*;
  5.  
  6. public class Students extends JFrame implements ActionListener
  7. {
  8. JLabel l_name, l_year, l_major, l_ft, l_email; // declaring Lebels for all inputs
  9. JTextField name, major, email; //declaring text field for input data
  10.  
  11.    String[] choice = {"Yes","No"};
  12.    String[] arrayYear = {"1","2","3","4"};
  13.    
  14.    JComboBox FullTime = new JComboBox(choice); //Declaring and creating combo box to select among yes or no
  15.    JComboBox year = new JComboBox(arrayYear); //Declaring and creating Combo box to select year
  16.    
  17.    
  18.   JButton Insert, Clear; //declaring button for insert and clear
  19.  
  20. Students()
  21. {
  22. setVisible(true);
  23. setSize(700, 700);
  24. setLayout(null);
  25. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  26. setTitle("Students"); //set the title of the frame
  27. l_name = new JLabel("Name:"); //adding text the lable
  28. l_year = new JLabel("Year:");//adding text the lable
  29. l_major = new JLabel("Major:");//adding text the lable
  30. l_ft = new JLabel("FullTime:");//adding text the lable
  31. l_email = new JLabel("Email:");//adding text the lable
  32. name = new JTextField(); //creating text field to input data
  33. major = new JTextField(); //creating text field to input data
  34. email = new JTextField(); //creating text field to input data
  35. Insert = new JButton("Insert"); //creating Button
  36. Clear = new JButton("Clear"); //creating Button
  37. Insert.addActionListener(this); //adding event listner to button "Insert"
  38. Clear.addActionListener(this); //adding event listner to button "Clear"
  39. add(l_name); //adding the element to frame
  40. add(name);//adding the element to frame
  41. add(l_year);//adding the element to frame
  42. add(year);//adding the element to frame
  43. add(l_major);//adding the element to frame
  44. add(major);//adding the element to frame
  45. add(l_ft);//adding the element to frame
  46. add(FullTime);//adding the element to frame
  47. add(l_email);//adding the element to frame
  48. add(email);//adding the element to frame
  49. add(Insert);//adding the element to frame
  50. add(Clear);//adding the element to frame
  51. }
  52.  
  53. public void actionPerformed(ActionEvent e)
  54. {
  55. if (e.getSource() == Insert) //If event is "Insert" i.e Insert button is clicked
  56. {
  57. String s1="jdbc:ucanaccess://";
  58. String user="";
  59. String pass="";
  60. String s2="students.accdb";
  61. String URL = s1+s2;
  62. ResultSet result;
  63. result = connectToDB(URL); printDB(result);
  64.  try
  65. {
  66. String database="classes.mdb";//Databse with full path. In this case database exists in the current directory
  67.                String url=("jdbc:ucanaccess:///Users/sherifatu.sumaila/mydb.mdb");              
  68.                Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  69.                Connection con=DriverManager.getConnection("jdbc:ucanaccess://Students.accdb");
  70.                Statement st=con.createStatement();
  71.                String sql = ("Insert Into StudentData (name,year,major,fullTime,email)" +
  72.                     "Values ('" +
  73.                     name.getText() + "','" + year.getSelectedIndex() + "','" + major.getText() + "','" +
  74.                     fullTime.getSelectedIndex() + "','" + email.getText() + "')";
  75.                    
  76.                     Statement statement = con.createStatement();
  77.            statement.execute(sql);
  78.            display("Student " + name.getText()+ " inserted successfully");
  79.            clear();
  80.        }
  81.        catch(Exception e) {
  82. System.out.println(e.getMessage());
  83. }
  84.  
  85. }
  86. else
  87. {
  88. name.setText("");//clearing the field
  89. major.setText("");//clearing the field
  90. email.setText("");//clearing the field
  91. }
  92. }
  93.  public void display(String msg) {
  94.  JOptionPane.showMessageDialog(null, msg);
  95.        
  96.        
  97.  }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement