Guest User

Untitled

a guest
Oct 19th, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7. import java.sql.ResultSet;
  8. import java.sql.Statement;
  9.  
  10. public class intermediatep {
  11. static String subject;// main variable which contains the subject
  12. // entered/selected
  13.  
  14. public static void main(String[] args) {
  15. try {
  16. // create a mysql database connection
  17. String myDriver = "com.mysql.jdbc.Driver";
  18. String myUrl = "jdbc:mysql://localhost:3307/quiz";
  19. Class.forName(myDriver);
  20. Connection conn = DriverManager.getConnection(myUrl, "root", "");
  21. String query = "select distinct subject from student";
  22. Statement st = conn.createStatement();
  23. ResultSet rs = st.executeQuery(query);
  24. int ctr = 0, i = 0;
  25. String sub[];
  26. while (rs.next()) {
  27. ctr++;
  28. }
  29.  
  30. sub = new String[ctr];
  31. while(rs.next()) {
  32. sub[i] = rs.getString("subject"); //takes only the unique values from the database due to the query sent
  33. i++;
  34. }
  35.  
  36. JFrame f = new JFrame("Quiz Portal");
  37. f.setSize(310, 300);
  38. JLabel l;
  39. l = new JLabel("Select Subject:\n");
  40. l.setBounds(50, 25, 100, 20);
  41. JTextArea t1, t2;
  42. JScrollPane p;
  43. t1 = new JTextArea();
  44. t1.setBounds(140, 25, 100, 20);
  45. DefaultListModel<String> s = new DefaultListModel<>();
  46.  
  47. for (i = 0; i < ctr; i++) {
  48. s.addElement(sub[i]);
  49. }
  50.  
  51. JList<String> list = new JList<>(s);
  52. // String j=list.getSelectedValue();
  53.  
  54. p = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
  55. JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
  56. p.setBounds(90, 80, 110, 90);
  57.  
  58. JButton b1 = new JButton("Start Quiz");
  59.  
  60. b1.addActionListener(new ActionListener()
  61.  
  62. {
  63.  
  64. public void actionPerformed(ActionEvent e) {
  65.  
  66. if (list.getSelectedValue() != null)
  67. subject = list.getSelectedValue().toString();
  68. else {
  69. subject = t1.getText();
  70. }
  71.  
  72. // create object for the next panel-menu obj = new menu();
  73. }
  74. });
  75. b1.setBounds(95, 200, 100, 20);
  76.  
  77. f.add(l);
  78. f.add(t1);
  79. f.add(p);
  80. f.add(b1);
  81. f.setLayout(null);
  82. f.setVisible(true);
  83. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  84. } catch (Exception e) {
  85. System.err.println("Got an exception!");
  86. System.err.println(e.getMessage());
  87.  
  88. }
  89. }
  90. }
Add Comment
Please, Sign In to add comment