Advertisement
Guest User

Untitled

a guest
May 4th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.32 KB | None | 0 0
  1.  
  2.  
  3. //----------------------------Java Imported Packages---------------------------------------
  4. import java.awt.BorderLayout;
  5. import java.awt.Color;
  6. import java.awt.Font;
  7. import java.awt.GridBagConstraints;
  8. import java.awt.GridBagLayout;
  9. import java.awt.Insets;
  10. import java.awt.event.*;
  11. import java.sql.*;
  12. import java.awt.*;
  13. import javax.swing.*;
  14.  
  15.  
  16. public class LogIn extends JFrame implements ActionListener, MouseListener
  17. {
  18. /**
  19. *
  20. */
  21. private static final long serialVersionUID = 1L;
  22.  
  23. JPanel UserPanel,NorthPanel;
  24.  
  25. JLabel UserId,UserPassword;
  26. JLabel LblFrgtPass;
  27.  
  28. JTextField TxtUser;
  29.  
  30. JPasswordField TxtUserPass;
  31.  
  32. JButton BtnLogin;
  33.  
  34. Connection con=null;
  35. Statement stmt=null;
  36. ResultSet rs=null;
  37. boolean flag=false;
  38. ResultSet rs2;
  39.  
  40.  
  41. public LogIn()
  42. {
  43. add(GetUserPanel(), BorderLayout.CENTER);
  44. add(GetNorthPanel(), BorderLayout.NORTH);
  45. }
  46.  
  47. public boolean GetConnection()
  48. {
  49. flag=false;
  50. try
  51. {
  52. Class.forName("com.mysql.cj.jdbc.Driver");
  53. con=DriverManager.getConnection("jdbc:mysql://localhost:3306/School", "root", "root");
  54. stmt=con.createStatement();
  55. flag=true;
  56. }catch(Exception ex)
  57. {
  58. ex.printStackTrace();
  59. flag=false;
  60. }
  61. return flag;
  62. }
  63.  
  64. public boolean CloseConnection()
  65. {
  66. flag=false;
  67. try
  68. {
  69. if(con!=null)
  70. {
  71. con.close();
  72. flag=true;
  73. }
  74. }catch(Exception ex)
  75. {
  76. flag=false;
  77. }
  78. return flag;
  79. }
  80.  
  81. public ResultSet GetRecords(String sql)
  82. {
  83. rs=null;
  84. try
  85. {
  86. rs=stmt.executeQuery(sql);
  87.  
  88. }catch(Exception ex)
  89. {
  90. rs=null;
  91. }
  92. return rs;
  93. }
  94.  
  95.  
  96. JPanel GetNorthPanel()
  97. {
  98. NorthPanel = new JPanel();
  99. NorthPanel.setLayout(new FlowLayout());
  100.  
  101. ImageIcon titleIcon = new ImageIcon("titlebanner1.png");
  102. JLabel title = new JLabel(titleIcon);
  103.  
  104. NorthPanel.add(title);
  105.  
  106. NorthPanel.setBackground(Color.white);
  107. return NorthPanel;
  108. }
  109.  
  110. JLabel GetLblForgetPassword()
  111. {
  112. LblFrgtPass = new JLabel("Forgot Password ? ");
  113. return LblFrgtPass;
  114. }
  115.  
  116.  
  117. JPanel GetUserPanel()
  118. {
  119. UserPanel=new JPanel();
  120. UserPanel.setLayout(new GridBagLayout());
  121. UserPanel.setBackground(Color.WHITE);
  122.  
  123.  
  124.  
  125. GridBagConstraints GbcUserId = new GridBagConstraints();
  126. GbcUserId.gridx=1;
  127. GbcUserId.gridy=3;
  128. GbcUserId.fill=GridBagConstraints.BOTH;
  129.  
  130. GbcUserId.insets = new Insets(10, 70, 0, 0);
  131. UserPanel.add(GetUserId(),GbcUserId);
  132.  
  133.  
  134.  
  135. GridBagConstraints GbcTxtUser = new GridBagConstraints();
  136. GbcTxtUser.gridx=2;
  137. GbcTxtUser.gridy=3;
  138.  
  139. GbcTxtUser.insets = new Insets(10, 40, 0, 0);
  140. UserPanel.add(GetTxtUser(),GbcTxtUser);
  141.  
  142.  
  143.  
  144. GridBagConstraints GbcUserPassword = new GridBagConstraints();
  145. GbcUserPassword.gridx=1;
  146. GbcUserPassword.gridy=4;
  147. GbcUserPassword.fill=GridBagConstraints.BOTH;
  148.  
  149. GbcUserPassword.insets = new Insets(10, 70, 0, 0);
  150. UserPanel.add(GetUserPassword(),GbcUserPassword);
  151.  
  152.  
  153.  
  154.  
  155. GridBagConstraints GbcTxtUserPass = new GridBagConstraints();
  156. GbcTxtUserPass.gridx=2;
  157. GbcTxtUserPass.gridy=4;
  158.  
  159. GbcTxtUserPass.insets = new Insets(10, 40, 0, 0);
  160. UserPanel.add(GetTxtUserPass(),GbcTxtUserPass);
  161.  
  162.  
  163.  
  164.  
  165. GridBagConstraints GbcBtnLogin = new GridBagConstraints();
  166. GbcBtnLogin.gridx=2;
  167. GbcBtnLogin.gridy=5;
  168.  
  169. GbcBtnLogin.insets = new Insets(50, 50, 20, 20);
  170. UserPanel.add(GetBtnLogin(),GbcBtnLogin);
  171.  
  172. GridBagConstraints GbcLblFrgtPass = new GridBagConstraints();
  173. GbcLblFrgtPass.gridx=3;
  174. GbcLblFrgtPass.gridy=5;
  175.  
  176. GbcLblFrgtPass.insets = new Insets(50, 0, 20, 20);
  177. //UserPanel.add(GetLblFrgtPass(),GbcLblFrgtPass);
  178.  
  179.  
  180.  
  181. return UserPanel;
  182. }
  183.  
  184. JLabel GetUserId()
  185. {
  186. UserId = new JLabel("User Id : ");
  187. UserId.setFont(new Font("Bookman Old Style", Font.PLAIN, 14));
  188. return UserId;
  189. }
  190.  
  191. JTextField GetTxtUser()
  192. {
  193. TxtUser = new JTextField(10);
  194. return TxtUser;
  195. }
  196.  
  197. JLabel GetUserPassword()
  198. {
  199. UserPassword = new JLabel("Password : ");
  200. UserPassword.setFont(new Font("Bookman Old Style", Font.PLAIN, 14));
  201. return UserPassword;
  202. }
  203.  
  204. JPasswordField GetTxtUserPass()
  205. {
  206. TxtUserPass = new JPasswordField(10);
  207. return TxtUserPass;
  208. }
  209.  
  210. JLabel GetLblFrgtPass()
  211. {
  212. LblFrgtPass = new JLabel("Forgot Passord ?");
  213. return LblFrgtPass;
  214. }
  215.  
  216. JButton GetBtnLogin()
  217. {
  218. BtnLogin = new JButton(" LogIn ");
  219. //Project1 p = new Project1();
  220. BtnLogin.addActionListener(this);
  221. BtnLogin.setFont(new Font("Bookman Old Style", Font.PLAIN, 14));
  222.  
  223.  
  224. return BtnLogin;
  225. }
  226.  
  227. public void actionPerformed(ActionEvent e)
  228. {
  229. if(e.getSource()==BtnLogin)
  230. {
  231. String User_Id = TxtUser.getText().trim();
  232. String User_Pass = TxtUserPass.getText().trim();
  233.  
  234. String userId="";
  235. String usertype="";
  236. String Fname = "";
  237. String Lname = "";
  238. //String sql = "Select * from Admin where UserId = '"+User_Id+"' and Password= '"+User_Pass+"'";
  239. String sql = " SELECT userid, UserType, Fname, Lname, password FROM admins WHERE UserId = '"+User_Id+"' and Password= '"+User_Pass+"' UNION SELECT userid, UserType, Fname, Lname, password FROM teachers WHERE UserId = '"+User_Id+"' and Password= '"+User_Pass+"' ";
  240.  
  241.  
  242. if(GetConnection()==true)
  243. {
  244. try
  245. {
  246. rs =GetRecords(sql);
  247. int count = 0;
  248.  
  249. while(rs.next())
  250.  
  251. {
  252. count = count + 1;
  253. userId =rs.getString(1);
  254. usertype=rs.getString(2);
  255. Fname = rs.getString(3);
  256. Lname = rs.getString(4);
  257. }
  258.  
  259. if(count == 1)
  260. {
  261. if(usertype.equalsIgnoreCase("teacher"))
  262. {
  263. stmt.executeUpdate(" delete from temp ");
  264. stmt.executeUpdate(" insert into temp values('"+userId+"', '"+Fname+"', '"+Lname+"', '"+usertype+"') ");
  265. JOptionPane.showMessageDialog(null, "Teacher");
  266. /*Teacher frame = new Teacher();
  267. //FRAME
  268. frame.setSize(600, 400);
  269. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  270. frame.setVisible(true);
  271. frame.setLocationRelativeTo(null);
  272. frame.setResizable(false);*/
  273. }
  274. else if(usertype.equalsIgnoreCase("admin"))
  275. {
  276. stmt.executeUpdate(" delete from temp ");
  277. stmt.executeUpdate(" insert into temp values('"+userId+"', '"+Fname+"', '"+Lname+"', '"+usertype+"') ");
  278. JOptionPane.showMessageDialog(null, "Admin");
  279. Admin frame = new Admin();
  280. //FRAME
  281. frame.setSize(600, 400);
  282. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  283. frame.setVisible(true);
  284. frame.setLocationRelativeTo(null);
  285. frame.setResizable(false);
  286. }
  287. dispose();
  288. }
  289. else
  290. {
  291. JOptionPane.showMessageDialog(null, "User Not Found!");
  292. }
  293.  
  294. }
  295. catch(Exception ex)
  296. {
  297. ex.printStackTrace();
  298. //System.err.println("ERROR2");
  299. }
  300. }
  301. else
  302. {
  303. System.out.println("Not Connected");
  304. }
  305. }
  306.  
  307.  
  308. }
  309.  
  310. public static void main(String[] args)
  311. {
  312. LogIn l = new LogIn();
  313. l.setVisible(true);
  314. l.setResizable(false);
  315. l.setSize(650,400);
  316. l.setLocationRelativeTo(null);
  317. l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  318. }
  319.  
  320. @Override
  321. public void mouseClicked(MouseEvent e) {
  322. // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  323. }
  324.  
  325. @Override
  326. public void mousePressed(MouseEvent e) {
  327. // throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  328. }
  329.  
  330. @Override
  331. public void mouseReleased(MouseEvent e) {
  332. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  333. }
  334.  
  335. @Override
  336. public void mouseEntered(MouseEvent e) {
  337. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  338. }
  339.  
  340. @Override
  341. public void mouseExited(MouseEvent e) {
  342. //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  343. }
  344. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement