Guest User

Untitled

a guest
Oct 15th, 2018
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.01 KB | None | 0 0
  1. import javax.swing.*;
  2. import java.awt.*;
  3. import java.awt.event.ActionListener;
  4. import java.sql.*;
  5. import java.util.Calendar;
  6.  
  7. //Assuming that the data table is for question, 4 options, correct answer
  8. public class JavaApplication17 {
  9.  
  10.  
  11. public static void calling(String[] args) //changed class name
  12. {
  13. sam obj=new sam();
  14. }
  15. }
  16. class sam extends JFrame
  17. {
  18.  
  19. static JTextArea t0;
  20. static JTextArea t1;
  21. static JTextArea t2;
  22. static JTextArea t3;
  23. static JTextArea t4;
  24. JRadioButton r1,r2,r3,r4;
  25. JLabel l,l5,l1,l2,l3,l4;
  26. JButton b;
  27. int i;
  28. JScrollPane pane= new JScrollPane(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS,JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  29.  
  30. public sam()
  31. {
  32. JFrame f=new JFrame("Teacher Interface");
  33. f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.  
  35.  
  36. f.setLayout(new FlowLayout());
  37.  
  38. f.setSize(600,500);
  39.  
  40. for(i=1;i<11;i++)
  41. {
  42.  
  43.  
  44. l5=new JLabel("Question "+i);
  45.  
  46.  
  47. l5.setPreferredSize(new Dimension(600, 40));
  48. l5.setHorizontalAlignment(JLabel.CENTER);
  49. l5.setVerticalAlignment(JLabel.CENTER);
  50. t0=new JTextArea(2,52);
  51. t0.getText();
  52. t1=new JTextArea(2,10);
  53. t1.getText();
  54.  
  55. t2=new JTextArea(2,10);
  56. t2.getText();
  57.  
  58. t3=new JTextArea(2,10);
  59. t3.getText();
  60.  
  61.  
  62. t4=new JTextArea(2,10);
  63. t4.getText();
  64.  
  65.  
  66. l=new JLabel("Correct Option");
  67. l1=new JLabel("A");
  68. l2=new JLabel("B");
  69. l3=new JLabel("C");
  70. l4=new JLabel("D");
  71. r1 =new JRadioButton("A");
  72. r2 =new JRadioButton("B");
  73. r3 =new JRadioButton("C");
  74. r4 =new JRadioButton("D");
  75.  
  76.  
  77. f.add(l5);
  78. f.add(t0);
  79.  
  80. f.add(l1);
  81. f.add(t1);
  82.  
  83. f.add(l2);
  84. f.add(t2);
  85. f.add(l3);
  86. f.add(t3);
  87. f.add(l4);
  88.  
  89. f.add(t4);
  90. f.add(l);
  91. f.add(r1);
  92. f.add(r2);
  93. f.add(r3);
  94. f.add(r4);
  95. ButtonGroup bg=new ButtonGroup();
  96. bg.add(r1);
  97. bg.add(r2);
  98. bg.add(r3);
  99. bg.add(r4);
  100.  
  101. }
  102.  
  103. //f.setContentPane(pane);
  104. b=new JButton("Submit");
  105. f.add(b);
  106. f.setVisible(true);
  107.  
  108. }
  109. public static void main(String[] args)
  110. {
  111. try
  112. {
  113. // create a mysql database connection
  114. String myDriver = "org.gjt.mm.mysql.Driver";
  115. String myUrl = "jdbc:mysql://localhost/test";
  116. Class.forName(myDriver);
  117. Connection conn = DriverManager.getConnection(myUrl, "root", "");
  118.  
  119.  
  120. // the mysql insert statement
  121. String query = " insert into users (question, subject, option 1, option 2, option 3, option 4, correct_opt)"
  122. + " values (?, ?, ?, ?, ?, ?, ?)";
  123.  
  124. // create the mysql insert preparedstatement
  125. PreparedStatement preparedStmt = conn.prepareStatement(query);
  126. preparedStmt.setString (1, t0.getText());
  127. //preparedStmt.setString(2, t5.getText()); assuming t5 will be the text input for subject
  128. preparedStmt.setString (3, t1.getText());
  129. preparedStmt.setString (4, t2.getText());
  130. preparedStmt.setString (5, t3.getText());
  131. preparedStmt.setString (6, t4.getText());
  132. if (r1.isSelected()); // to check if the radio button selected is correct or not
  133. else if (r2.isSelected());
  134. else if (r3.isSelected());
  135. else if(r4.isSelected());
  136. //the statement will be preparedStmt.setInteger (7, w/e the integer is);
  137. // execute the preparedstatement
  138. preparedStmt.execute();
  139.  
  140. conn.close();
  141. }
  142. catch (Exception e)
  143. {
  144. System.err.println("Got an exception!");
  145. System.err.println(e.getMessage());
  146. }
  147. }
  148. }
Add Comment
Please, Sign In to add comment