Guest User

Untitled

a guest
Sep 6th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. LAB 2 : Login
  2.  
  3. package loginP;
  4.  
  5.  
  6. import java.awt.*;
  7. import java.applet.*;
  8. import java.awt.event.*;
  9. import java.sql.Connection;
  10. import java.sql.DriverManager;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14.  
  15. import com.mysql.jdbc.PreparedStatement;
  16.  
  17.  
  18. public class loginP extends Applet implements ActionListener
  19. {
  20. /**
  21. *
  22. */
  23. private static final long serialVersionUID = 1L;
  24. Applet second;
  25. TextField pass,name;
  26. Connection con;
  27. String url="jdbc:mysql://localhost/user",uname="root",passw="student";
  28. Button b1,b2;
  29. public void init()
  30. {
  31. try
  32. {
  33. Class.forName("com.mysql.jdbc.Driver");
  34. con= DriverManager.getConnection(url, uname, passw);
  35. Label n=new Label("Name:",Label.CENTER);
  36. Label p=new Label("password:",Label.CENTER);
  37. name=new TextField(20);
  38. pass=new TextField(20);
  39. pass.setEchoChar('*');
  40. b1=new Button("submit");
  41. b2=new Button("register");
  42. add(n);
  43. add(name);
  44. add(p);
  45. add(pass);
  46. add(b1);
  47. add(b2);
  48. n.setBounds(70,90,90,60);
  49. p.setBounds(70,130,90,60);
  50. name.setBounds(280,100,90,20);
  51. pass.setBounds(200,140,90,20);
  52. b1.setBounds(100,260,70,40);
  53. b2.setBounds(180,260,70,40);
  54. b1.addActionListener(this);
  55. b2.addActionListener(this);
  56. }
  57. catch(Exception ae)
  58. {
  59. ae.printStackTrace();
  60. }
  61.  
  62. }
  63. public void paint(Graphics g)
  64. {
  65. repaint();
  66. }
  67. @Override
  68. public void actionPerformed(ActionEvent e) {
  69. if (e.getSource() == b1)
  70. {
  71.  
  72. try {
  73. Statement ps1=con.createStatement();
  74. String sql="select name,password from login";
  75. ResultSet rs = ps1.executeQuery(sql);
  76. String un,pw;
  77. un=name.getText();
  78.  
  79. pw=pass.getText();
  80. int f=0,g=0;
  81. while(rs.next())
  82. {
  83. if((rs.getString(1).compareTo(un)==0) )
  84. {
  85. f=1;
  86. if((rs.getString(2).compareTo(pw)==0) )
  87. g=1;
  88. break;
  89. }
  90.  
  91.  
  92. }
  93. if(g==1&&f==1)
  94. {
  95. System.out.println("Welcome "+un+".!!!!");
  96. }
  97.  
  98. else if(g==1&f==0)
  99. {
  100. System.out.println("Wrong password!!");
  101. }
  102. else
  103. System.out.println("User does not exist!!");
  104.  
  105.  
  106. }
  107. catch (SQLException e1) {
  108. // TODO Auto-generated catch block
  109. e1.printStackTrace();
  110. } }
  111.  
  112. else
  113. {
  114.  
  115. System.out.println("Button 2 was pressed");
  116. String uname,passw;
  117. uname=name.getText();
  118. passw=pass.getText();
  119. String a="INSERT into login values(?,?,?,?)";
  120. try{
  121. Statement ps1=con.createStatement();
  122. PreparedStatement pstmt = (PreparedStatement) con.prepareStatement(a);
  123. pstmt.setString(1,uname);
  124. pstmt.setString(2, passw);
  125. pstmt.setInt(3,25);
  126. pstmt.setString(4,"Teacher");
  127. pstmt.executeUpdate();
  128. System.out.println("Record is inserted into LOGIN table!");
  129. }catch(SQLException e1) {
  130. System.out.println(e1.getMessage());
  131. }
  132. second=null;
  133. second= getAppletContext().getApplet("Register");
  134. }
  135.  
  136. }
  137.  
  138. }
Add Comment
Please, Sign In to add comment