Advertisement
Guest User

Untitled

a guest
Dec 21st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. package foodbankdatabase3;
  2.  
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.sql.*;
  6. import javax.swing.*;
  7.  
  8. public class FoodBankDatabase extends JFrame{
  9. /*
  10. Initializing Variables
  11. */
  12. static String testConnect;
  13. static Connection conn;
  14. static PreparedStatement pst;
  15. static ResultSet rs;
  16. /*
  17. Initializing JPanels
  18. */
  19. JPanel contPanel = new JPanel();
  20. JPanel loginGui = new JPanel();
  21. JPanel usernamePanel = new JPanel();
  22. JPanel passwordPanel = new JPanel();
  23. JPanel mainMenuGui = new JPanel();
  24. JPanel loginButtonPanel = new JPanel();
  25. JPanel testConnectPanel = new JPanel();
  26. /*
  27. Initializing JLabels
  28. */
  29. JLabel usernameLabel = new JLabel("Username: ");
  30. JLabel passwordLabel = new JLabel("Password: ");
  31. JLabel testConnectLabel = new JLabel(testConnect);
  32. /*
  33. Initializing JTextFields
  34. */
  35. JTextField usernameTextField = new JTextField(10);
  36. JPasswordField passwordTextField = new JPasswordField(10);
  37. /*
  38. Initializing JButtons
  39. */
  40. JButton loginButton = new JButton("Click here to login");
  41. JButton logoutButton = new JButton("Click here to logout");
  42. JButton conductInterview = new JButton("Conduct an interview");
  43. JButton addClient = new JButton("Add a new client");
  44. JButton viewClient = new JButton("View existing client details");
  45.  
  46.  
  47. CardLayout cardLayout = new CardLayout();
  48.  
  49. public FoodBankDatabase(){
  50. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51. initComponents();
  52. pack();
  53. setVisible(true);
  54. }
  55.  
  56. private void initComponents(){
  57. contPanel.setLayout(cardLayout);
  58.  
  59. /*
  60. Login Gui Components
  61. */
  62. loginGui.setLayout(new BoxLayout(loginGui, BoxLayout.Y_AXIS));
  63. usernamePanel.add(usernameLabel);
  64. usernamePanel.add(usernameTextField);
  65. passwordPanel.add(passwordLabel);
  66. passwordPanel.add(passwordTextField);
  67. loginButtonPanel.add(loginButton);
  68. testConnectPanel.add(testConnectLabel);
  69. loginGui.add(usernamePanel);
  70. loginGui.add(passwordPanel);
  71. loginGui.add(loginButtonPanel);
  72. loginGui.add(testConnectPanel);
  73.  
  74. /*
  75. Main Menu Gui Components
  76. */
  77. mainMenuGui.setLayout(new BoxLayout(mainMenuGui, BoxLayout.Y_AXIS));
  78. mainMenuGui.add(conductInterview);
  79. mainMenuGui.add(addClient);
  80. mainMenuGui.add(viewClient);
  81. mainMenuGui.add(logoutButton);
  82.  
  83. contPanel.add(loginGui, "loginGui");
  84. contPanel.add(mainMenuGui, "mainMenuGui");
  85.  
  86. cardLayout.show(contPanel, "loginGui");
  87.  
  88. loginButton.addActionListener(new ActionListener() {
  89. @Override
  90. public void actionPerformed(ActionEvent e) {
  91. String sql="SELECT * FROM loginDetails WHERE username=? AND password=?";
  92. try{
  93. pst = conn.prepareStatement(sql);
  94. pst.setString(1, usernameTextField.getText());
  95. pst.setString(1, passwordTextField.getText());
  96.  
  97. rs=pst.executeQuery();
  98. if(rs.next()){
  99. JOptionPane.showMessageDialog(null, "Username and Password are correct");
  100. }else{
  101. JOptionPane.showMessageDialog(null, "Username and Password are not correct");
  102. }
  103.  
  104. }catch(Exception sqle){
  105. JOptionPane.showMessageDialog(null, sqle);
  106. }
  107. }
  108. });
  109.  
  110. logoutButton.addActionListener(new ActionListener() {
  111. @Override
  112. public void actionPerformed(ActionEvent e) {
  113. cardLayout.show(contPanel, "loginGui");
  114. }
  115. });
  116.  
  117. add(contPanel);
  118. }
  119.  
  120. private static void testConnection(){
  121. try{
  122. Connection con = DriverManager.getConnection("jdbc:mysql://sql5.freemysqlhosting.net:3306/sql562010", "sql562010", "bV5*yJ5%");
  123.  
  124. Statement stat = (Statement) con.createStatement();
  125.  
  126. testConnect = "Connection successful!";
  127. }catch(Exception e){
  128. testConnect = "Connection unsuccessful";
  129. System.out.println(e);
  130. }
  131. }
  132.  
  133. public static void main(String[] args) {
  134. testConnection();
  135. new FoodBankDatabase();
  136. }
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement