Guest User

Untitled

a guest
Jul 2nd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.77 KB | None | 0 0
  1. `package hM_prac;
  2. import java.awt.BorderLayout;
  3. import java.awt.EventQueue;
  4.  
  5. import javax.swing.JFrame;
  6. import javax.swing.JOptionPane;
  7. import javax.swing.JPanel;
  8. import javax.swing.border.EmptyBorder;
  9. import javax.swing.JLabel;
  10.  
  11.  
  12. import java.awt.Font;
  13.  
  14. import javax.swing.SwingConstants;
  15. import javax.swing.JTextField;
  16. import javax.swing.border.LineBorder;
  17.  
  18. import java.awt.Color;
  19. import java.awt.event.ActionEvent;
  20. import java.awt.event.ActionListener;
  21.  
  22. import javax.swing.JButton;
  23. import javax.swing.JPasswordField;
  24.  
  25. import java.sql.*;
  26. public class login extends JFrame implements ActionListener {
  27.  
  28. private JPanel contentPane;
  29. private JTextField txtUsername;
  30. private JPasswordField pwdwhatever;
  31. JLabel lblNewLabel,lblNewLabel_1,lblNewLabel_2;
  32. JButton btnForgotPassword ,btnSignUp,btnLogin;
  33.  
  34. /**
  35. * Launch the application.
  36. */
  37. public static void main(String[] args) {
  38. EventQueue.invokeLater(new Runnable() {
  39. public void run() {
  40. try {
  41. login frame = new login();
  42. frame.setVisible(true);
  43. } catch (Exception e) {
  44. e.printStackTrace();
  45. }
  46. }
  47. });
  48. }
  49. Connection con;
  50. PreparedStatement ps;
  51. ResultSet rs;
  52.  
  53. /**
  54. * Create the frame.
  55. */
  56. public login() {
  57. super("LOGIN");
  58. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  59. setBounds(100, 100, 465, 300);
  60. contentPane = new JPanel();
  61. contentPane.setBorder(new LineBorder(new Color(0, 0, 0)));
  62. setContentPane(contentPane);
  63. contentPane.setLayout(null);
  64.  
  65. lblNewLabel = new JLabel("Welcome to MYHOTEL");
  66. lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
  67. lblNewLabel.setFont(new Font("Verdana", Font.PLAIN, 18));
  68. lblNewLabel.setBounds(10, 11, 414, 23);
  69. contentPane.add(lblNewLabel);
  70.  
  71. txtUsername = new JTextField();
  72. txtUsername.setText("");
  73. txtUsername.setBounds(222, 96, 94, 20);
  74. contentPane.add(txtUsername);
  75. txtUsername.setColumns(20);
  76.  
  77. lblNewLabel_1 = new JLabel("Username:");
  78. lblNewLabel_1.setLabelFor(txtUsername);
  79. lblNewLabel_1.setBounds(121, 99, 69, 14);
  80. contentPane.add(lblNewLabel_1);
  81.  
  82. lblNewLabel_2 = new JLabel("Password:");
  83. lblNewLabel_2.setBounds(121, 138, 69, 14);
  84. contentPane.add(lblNewLabel_2);
  85.  
  86. btnLogin = new JButton("Login");
  87. btnLogin.setBounds(169, 166, 89, 23);
  88. contentPane.add(btnLogin);
  89.  
  90. btnSignUp = new JButton("Sign Up");
  91. btnSignUp.setBounds(305, 215, 89, 23);
  92. contentPane.add(btnSignUp);
  93.  
  94. btnForgotPassword = new JButton("Forgot password");
  95. btnForgotPassword.setBounds(37, 215, 135, 23);
  96. contentPane.add(btnForgotPassword);
  97.  
  98. pwdwhatever = new JPasswordField();
  99. pwdwhatever.setText("");
  100. pwdwhatever.setBounds(222, 136, 94, 17);
  101. contentPane.add(pwdwhatever);
  102.  
  103. btnLogin.addActionListener(this);
  104. btnSignUp.addActionListener(this);
  105. btnForgotPassword.addActionListener(this);
  106. }
  107. @Override
  108. public void actionPerformed(ActionEvent ae) {
  109. // TODO Auto-generated method stub
  110.  
  111. Object o=ae.getSource();
  112. if(o==btnLogin)
  113. {
  114. String uname,pass;
  115. uname=txtUsername.getText();
  116. pass=pwdwhatever.getText();
  117. try
  118. {
  119. Class.forName("oracle.jdbc.driver.OracleDriver");
  120. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
  121. ps=con.prepareStatement("select * from account where username=? and password=?");
  122. ps.setString(1,uname);
  123. ps.setString(2,pass);
  124. rs=ps.executeQuery();
  125. if(rs.next())
  126. {
  127. System.out.println("Valid User.");
  128. JOptionPane.showMessageDialog(this,"Valid User","Done",JOptionPane.INFORMATION_MESSAGE);
  129. this.dispose();
  130. rs.close();
  131. ps.close();
  132. //new homepage();
  133. }
  134. else
  135. {
  136. JOptionPane.showMessageDialog(this,"Incorrect","Error",JOptionPane.ERROR_MESSAGE);
  137. }
  138. }
  139. catch(Exception e)
  140. {
  141. System.out.println(e);
  142. }
  143. }
  144. if(o==btnSignUp)
  145. {
  146. this.dispose();
  147. new sign_up().setVisible(true);
  148. }
  149. if(o==btnForgotPassword)
  150. {
  151. this.dispose();
  152. new forget().setVisible(true);
  153. }
  154. }
  155. }
  156. `
  157.  
  158. ` package hM_prac;
  159.  
  160. import java.awt.EventQueue;
  161.  
  162. import javax.swing.JFrame;
  163.  
  164. import java.awt.GridLayout;
  165.  
  166. import javax.swing.JLabel;
  167. import javax.swing.SwingConstants;
  168.  
  169. import java.awt.Font;
  170.  
  171. import javax.swing.JOptionPane;
  172. import javax.swing.JPanel;
  173. import javax.swing.JTextField;
  174. import javax.swing.JButton;
  175. import javax.swing.JPasswordField;
  176. import javax.swing.border.EmptyBorder;
  177.  
  178. import java.awt.Color;
  179. import java.awt.event.ActionEvent;
  180. import java.awt.event.ActionListener;
  181. import java.sql.*;
  182. public class forget extends JFrame implements ActionListener {
  183.  
  184. private JFrame frame;
  185. private JTextField textField;
  186. private JTextField textField_1;
  187. private JPasswordField passwordField;
  188. JButton btnSubmit,btnSubmitAnswer;
  189. JLabel label,lblNewLabel_3;
  190. /**
  191. * Launch the application.
  192. */
  193. public static void main(String[] args) {
  194. EventQueue.invokeLater(new Runnable() {
  195. public void run() {
  196. try {
  197. System.out.println("hh");
  198. forget window = new forget();
  199. window.frame.setVisible(true);
  200. } catch (Exception e) {
  201. e.printStackTrace();
  202. }
  203. }
  204. });
  205. }
  206.  
  207. /**
  208. * Create the application.
  209. */
  210. public forget() {
  211. //super("login");
  212. initialize();
  213. }
  214.  
  215. /**
  216. * Initialize the contents of the frame.
  217. */
  218. private void initialize() {
  219. frame = new JFrame();
  220. frame.setBounds(100, 100, 374, 358);
  221. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  222. frame.getContentPane().setLayout(null);
  223.  
  224. JLabel lblNewLabel = new JLabel("Username:");
  225. lblNewLabel.setBackground(Color.BLACK);
  226. lblNewLabel.setBounds(18, 77, 92, 14);
  227. frame.getContentPane().add(lblNewLabel);
  228.  
  229. JLabel lblNewLabel_1 = new JLabel("Security Question:");
  230. lblNewLabel_1.setBackground(Color.BLACK);
  231. lblNewLabel_1.setBounds(18, 139, 132, 14);
  232. frame.getContentPane().add(lblNewLabel_1);
  233.  
  234. JLabel lblNewLabel_2 = new JLabel("Answer:");
  235. lblNewLabel_2.setBackground(Color.BLACK);
  236. lblNewLabel_2.setBounds(18, 164, 92, 14);
  237. frame.getContentPane().add(lblNewLabel_2);
  238.  
  239.  
  240. //frame.getContentPane().add(lblNewLabel_3);
  241.  
  242. JLabel lblNewLabel_5 = new JLabel("Forgot password");
  243. lblNewLabel_5.setFont(new Font("Tahoma", Font.BOLD, 19));
  244. lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
  245. lblNewLabel_5.setBounds(10, 11, 338, 23);
  246. frame.getContentPane().add(lblNewLabel_5);
  247.  
  248. textField = new JTextField();
  249. textField.setBackground(Color.WHITE);
  250. textField.setBounds(156, 77, 182, 14);
  251. frame.getContentPane().add(textField);
  252. textField.setColumns(10);
  253.  
  254. btnSubmit = new JButton("Submit ");
  255. btnSubmit.setBackground(Color.WHITE);
  256. btnSubmit.setBounds(184, 102, 142, 23);
  257. frame.getContentPane().add(btnSubmit);
  258.  
  259. label = new JLabel("..");
  260. label.setBackground(Color.BLACK);
  261. label.setBounds(160, 139, 178, 14);
  262. frame.getContentPane().add(label);
  263.  
  264. textField_1 = new JTextField();
  265. textField_1.setBounds(156, 164, 192, 14);
  266. frame.getContentPane().add(textField_1);
  267. textField_1.setColumns(10);
  268.  
  269. btnSubmitAnswer = new JButton("Submit Answer");
  270. btnSubmitAnswer.setBackground(Color.WHITE);
  271. btnSubmitAnswer.setBounds(184, 189, 142, 23);
  272. frame.getContentPane().add(btnSubmitAnswer);
  273.  
  274.  
  275. //frame.getContentPane().add(btnSubmitPassword);
  276.  
  277.  
  278.  
  279. btnSubmit.addActionListener(this);
  280. btnSubmitAnswer.addActionListener(this);
  281.  
  282. }
  283.  
  284.  
  285. Connection con;
  286. PreparedStatement ps;
  287. ResultSet rs;
  288. private JButton btnSubmitPassword;
  289.  
  290. public void actionPerformed(ActionEvent ae) {
  291. Object o=ae.getSource();
  292. String uname;
  293. if(o==btnSubmit)
  294. { System.out.println("ques");
  295. uname=textField.getText();
  296. try
  297. {
  298. Class.forName("oracle.jdbc.driver.OracleDriver");
  299. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
  300.  
  301. ps=con.prepareStatement("select security_question from account where username=?");
  302. ps.setString(1,uname);
  303. rs=ps.executeQuery();
  304. if(rs.next())
  305. {
  306. String secq=rs.getString("security_question");
  307. System.out.println("question"+secq);
  308. label.setText(secq);
  309. frame.getContentPane().add(label);
  310. }
  311. else if(rs==null){
  312. JOptionPane.showMessageDialog(this,"No such user","Error",JOptionPane.ERROR_MESSAGE);
  313. }
  314. else
  315. {
  316. JOptionPane.showMessageDialog(this,"Invalid User","Error",JOptionPane.ERROR_MESSAGE);
  317. }
  318.  
  319. }
  320. catch(Exception e)
  321. {
  322. System.out.println(e);
  323. }
  324. }
  325. else if(o==btnSubmitAnswer)
  326. {
  327. //System.out.println("jj");
  328. String ans;
  329. uname=textField.getText();
  330. ans=textField_1.getText();
  331. try
  332. {
  333. Class.forName("oracle.jdbc.driver.OracleDriver");
  334. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
  335. ps=con.prepareStatement("select * from account where username=? and answer=?");
  336. ps.setString(1,uname);
  337. ps.setString(2,ans);
  338. rs=ps.executeQuery();
  339. if(rs!=null)
  340. {
  341. System.out.println("jj");
  342. lblNewLabel_3 = new JLabel("New Password:");
  343. lblNewLabel_3.setBackground(Color.BLACK);
  344. lblNewLabel_3.setBounds(18, 226, 117, 14);
  345. frame.getContentPane().add(lblNewLabel_3);
  346.  
  347. passwordField = new JPasswordField();
  348. passwordField.setBackground(Color.BLACK);
  349. passwordField.setBounds(160, 226, 178, 17);
  350. //frame.getContentPane().add(passwordField);
  351. frame.getContentPane().add(passwordField);
  352.  
  353. btnSubmitPassword = new JButton("Submit password");
  354. btnSubmitPassword.setBounds(82, 285, 182, 23);
  355. frame.getContentPane().add(btnSubmitPassword);
  356.  
  357. btnSubmitPassword.addActionListener(this);
  358. }
  359. else{
  360. JOptionPane.showMessageDialog(this,"Wrong answer","Error",JOptionPane.ERROR_MESSAGE);
  361. }
  362.  
  363. }catch(Exception e){
  364. e.printStackTrace();
  365. }
  366.  
  367. }
  368. else if(o==btnSubmitPassword){
  369. String pass;
  370. uname=textField.getText();
  371. pass=passwordField.getText();
  372. try
  373. {
  374. Class.forName("oracle.jdbc.driver.OracleDriver");
  375. con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","hm","hm");
  376. ps=con.prepareStatement("update account set password=? where username=?");
  377. ps.setString(1,pass);
  378. ps.setString(1,uname);
  379. rs=ps.executeQuery();
  380. if(rs==null)
  381. {
  382. JOptionPane.showMessageDialog(this,"Error","Error",JOptionPane.ERROR_MESSAGE);
  383. }
  384. else{
  385. JOptionPane.showMessageDialog(this,"Password reset","Error",JOptionPane.ERROR_MESSAGE);
  386. this.dispose();
  387. new login();
  388. }}catch(Exception e){
  389. e.printStackTrace();
  390. }
  391. }
  392. }
  393. }
  394.  
  395.  
  396. `
Add Comment
Please, Sign In to add comment