Guest User

Untitled

a guest
Jul 31st, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. Java MySQL Select Query
  2. public class Connector {
  3. Connection con;
  4. PreparedStatement stmt;
  5. ResultSet rs;
  6.  
  7. Connector()
  8. {
  9. try{
  10.  
  11.  
  12. Class.forName("com.mysql.jdbc.Driver");
  13.  
  14. con=DriverManager.getConnection("jdbc:mysql://localhost/luxmidatabase","root","");
  15. stmt=con.prepareStatement("select * from login where username=? and password=?");
  16. }
  17.  
  18. catch (Exception e)
  19. {
  20. System.out.println(e);
  21. }
  22. }
  23. }
  24.  
  25. public class AttendanceHistory extends Connector
  26. {
  27.  
  28. public AttendanceHistory() {
  29. initialize();
  30. }
  31.  
  32. public JFrame frmAttendanceHistory;
  33.  
  34. Connector con;
  35. private JTextField textField;
  36.  
  37. // initialise the frame
  38. private void initialize() {
  39. frmAttendanceHistory = new JFrame();
  40. frmAttendanceHistory.setTitle("LEC AdminPro: Attendance History");
  41. frmAttendanceHistory.setBounds(100, 100, 323, 149);
  42. frmAttendanceHistory.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  43. frmAttendanceHistory.setLocationRelativeTo(null);
  44. // Initialising a Grey Border, which I am going to be using later to add borders to JLabels.
  45. Border border = LineBorder.createGrayLineBorder();
  46. frmAttendanceHistory.getContentPane().setLayout(null);
  47. con = new Connector();
  48.  
  49. JPanel panel_1 = new JPanel();
  50. panel_1.setBounds(0, 0, 307, 113);
  51. frmAttendanceHistory.getContentPane().add(panel_1);
  52. panel_1.setBorder(border);
  53. panel_1.setLayout(null);
  54.  
  55. JLabel lblRegister = new JLabel("View Attendance History");
  56. lblRegister.setFont(new Font("Tahoma", Font.BOLD, 12));
  57. lblRegister.setBounds(10, 11, 193, 20);
  58. panel_1.add(lblRegister);
  59.  
  60. JLabel lblStudentId = new JLabel("Student ID");
  61. lblStudentId.setFont(new Font("Arial", Font.PLAIN, 11));
  62. lblStudentId.setBounds(10, 45, 69, 14);
  63. panel_1.add(lblStudentId);
  64.  
  65. // Created a Cancel button along with an OptionDialog which pops up to confirm whether user wants to cancel registration or not.
  66. JButton btnNewButton = new JButton("Cancel");
  67. btnNewButton.addActionListener(new ActionListener() {
  68. public void actionPerformed(ActionEvent arg0) {
  69. Object[] options = {"Yes", "No"};
  70. Component form = null;
  71. int n = JOptionPane.showOptionDialog(form, "Would you like to cancel the progress?", "Exit Confirmation", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options);
  72. if(n == JOptionPane.YES_OPTION) {
  73. frmAttendanceHistory.setVisible(false);
  74. }
  75.  
  76. }
  77. });
  78. btnNewButton.setBounds(159, 73, 75, 23);
  79. panel_1.add(btnNewButton);
  80.  
  81. JButton button = new JButton("Submit");
  82. button.addActionListener(new ActionListener() {
  83. public void actionPerformed(ActionEvent arg0) {
  84.  
  85.  
  86. //String studentID = textField.getText();
  87.  
  88. try {
  89.  
  90.  
  91. con.stmt.executeQuery("SELECT StudentID, date FROM attendance WHERE StudentID = 21457");
  92. while ( rs.next() ) {
  93. String date = rs.getString("date");
  94. System.out.println(date);
  95. }
  96.  
  97. JOptionPane.showMessageDialog(frmAttendanceHistory, "Attendance has been registered.");
  98. frmAttendanceHistory.setVisible(false);
  99.  
  100. } catch (SQLException e) {
  101. //System.out.println("Record couldn't be added!");
  102. JOptionPane.showMessageDialog(frmAttendanceHistory, "Attendance couldn't be registered. Please try again!");
  103. e.printStackTrace();
  104. }
  105. }
  106. });
  107.  
  108.  
  109.  
  110. button.setBounds(81, 73, 75, 23);
  111. panel_1.add(button);
  112.  
  113. textField = new JTextField();
  114. textField.setBounds(81, 42, 212, 20);
  115. panel_1.add(textField);
  116. textField.setColumns(10);
  117.  
  118.  
  119.  
  120.  
  121. }
  122.  
  123.  
  124.  
  125.  
  126. public static void main(String[] args) {
  127. EventQueue.invokeLater(new Runnable() {
  128.  
  129. public void run() {
  130. try {
  131. AttendanceHistory window = new AttendanceHistory();
  132. window.frmAttendanceHistory.setVisible(true);
  133. } catch (Exception e) {
  134. e.printStackTrace();
  135. }
  136. }
  137. });
  138. }
  139.  
  140.  
  141.  
  142. // method to make this frame visible (may be used in other classes for retrieval)
  143. public void setVisible(boolean b) {
  144. frmAttendanceHistory.setVisible(true);
  145.  
  146. }
  147. }
  148.  
  149. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
  150. at AttendanceHistory$2.actionPerformed(AttendanceHistory.java:85)
  151. at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  152. at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  153. at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  154. at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  155. at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
  156. at java.awt.Component.processMouseEvent(Unknown Source)
  157. at javax.swing.JComponent.processMouseEvent(Unknown Source)
  158. at java.awt.Component.processEvent(Unknown Source)
  159. at java.awt.Container.processEvent(Unknown Source)
  160. at java.awt.Component.dispatchEventImpl(Unknown Source)
  161. at java.awt.Container.dispatchEventImpl(Unknown Source)
  162. at java.awt.Component.dispatchEvent(Unknown Source)
  163. at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  164. at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  165. at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  166. at java.awt.Container.dispatchEventImpl(Unknown Source)
  167. at java.awt.Window.dispatchEventImpl(Unknown Source)
  168. at java.awt.Component.dispatchEvent(Unknown Source)
  169. at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  170. at java.awt.EventQueue.access$000(Unknown Source)
  171. at java.awt.EventQueue$1.run(Unknown Source)
  172. at java.awt.EventQueue$1.run(Unknown Source)
  173. at java.security.AccessController.doPrivileged(Native Method)
  174. at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
  175. at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
  176. at java.awt.EventQueue$2.run(Unknown Source)
  177. at java.awt.EventQueue$2.run(Unknown Source)
  178. at java.security.AccessController.doPrivileged(Native Method)
  179. at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
  180. at java.awt.EventQueue.dispatchEvent(Unknown Source)
  181. at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  182. at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  183. at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  184. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  185. at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  186. at java.awt.EventDispatchThread.run(Unknown Source)
  187.  
  188. while ( rs.next() ) {
  189.  
  190. ResultSet rs = con.stmt.executeQuery(...);
  191.  
  192. Statement st = con.con.createStatement();
  193. ResultSet rs = st.executeQuery("SELECT StudentID, date from ...");
  194. while (rs.hasNext()) {
  195. // ...
  196. }
Add Comment
Please, Sign In to add comment