Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.Statement;
- public class intermediatep {
- static String subject;// main variable which contains the subject
- // entered/selected
- public static void main(String[] args) {
- try {
- // create a mysql database connection
- String myDriver = "com.mysql.jdbc.Driver";
- String myUrl = "jdbc:mysql://localhost:3307/quiz";
- Class.forName(myDriver);
- Connection conn = DriverManager.getConnection(myUrl, "root", "");
- String query = "select distinct subject from student";
- Statement st = conn.createStatement();
- ResultSet rs = st.executeQuery(query);
- int ctr = 0, i = 0;
- String sub[];
- while (rs.next()) {
- ctr++;
- }
- sub = new String[ctr];
- while(rs.next()) {
- sub[i] = rs.getString("subject"); //takes only the unique values from the database due to the query sent
- i++;
- }
- JFrame f = new JFrame("Quiz Portal");
- f.setSize(310, 300);
- JLabel l;
- l = new JLabel("Select Subject:\n");
- l.setBounds(50, 25, 100, 20);
- JTextArea t1, t2;
- JScrollPane p;
- t1 = new JTextArea();
- t1.setBounds(140, 25, 100, 20);
- DefaultListModel<String> s = new DefaultListModel<>();
- for (i = 0; i < ctr; i++) {
- s.addElement(sub[i]);
- }
- JList<String> list = new JList<>(s);
- // String j=list.getSelectedValue();
- p = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,
- JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
- p.setBounds(90, 80, 110, 90);
- JButton b1 = new JButton("Start Quiz");
- b1.addActionListener(new ActionListener()
- {
- public void actionPerformed(ActionEvent e) {
- if (list.getSelectedValue() != null)
- subject = list.getSelectedValue().toString();
- else {
- subject = t1.getText();
- }
- // create object for the next panel-menu obj = new menu();
- }
- });
- b1.setBounds(95, 200, 100, 20);
- f.add(l);
- f.add(t1);
- f.add(p);
- f.add(b1);
- f.setLayout(null);
- f.setVisible(true);
- f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- } catch (Exception e) {
- System.err.println("Got an exception!");
- System.err.println(e.getMessage());
- }
- }
- }
Add Comment
Please, Sign In to add comment