Guest User

Untitled

a guest
Jul 30th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. This is the code I'm using innetbeans with mysql db. The form is accepting user input(name and password) and successfully inserting it to the db table. But when I'm trying to login into the form using the name and password where keeping the name fixed I want to compare the password and if the password field match for the particular name then only to login . This is showing Exception:
  2.  
  3. public class Trying extends JFrame{
  4. Trying(){
  5. //declaration of fields and buttons
  6. // adding name and password in mysql table
  7. jb.addActionListener(new ActionListener(){
  8. public void actionPerformed(ActionEvent ae){
  9. try{
  10. Class.forName("com.mysql.jdbc.Driver");
  11. Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql", "root", "");
  12. String query="INSERT INTO tryingtable (name,password) VALUES (?,?)";
  13. PreparedStatement prepstmt=conn.prepareStatement(query);
  14. prepstmt.setString(1, jtf.getText());
  15. char[] password = jpf.getPassword();
  16. String pass = new String(password);
  17. prepstmt.setString(2,pass);
  18. prepstmt.execute();
  19. JOptionPane.showMessageDialog(null, "Done");
  20. prepstmt.close();
  21. }
  22. catch(Exception e){
  23. System.out.println("Exception on submit:"+e.getMessage());
  24. }
  25. }
  26. });
  27. // user will enter passsword and name and we will check in mysql table if the password in correct for the particular name in mysql table
  28. jb1.addActionListener(new ActionListener(){
  29. public void actionPerformed(ActionEvent ae){
  30. try{
  31. Class.forName("com.mysql.jdbc.Driver");
  32. Connection conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","");
  33. String query="SELECT password FROM trytable WHERE name=?;";
  34. PreparedStatement prepstmt=conn.prepareStatement(query);
  35. prepstmt.setString(1,jtf.getText());
  36. ResultSet rs=prepstmt.executeQuery();
  37. if(rs.next()){
  38. String result=rs.getString(query);
  39. char[] pass = jpf.getPassword();
  40. String password1= Arrays.toString(pass);
  41. if(result.equals(password1)){JOptionPane.showMessageDialog(null, "logged in");}
  42. else{JOptionPane.showMessageDialog(null, "Sorry");}
  43. }
  44. }
  45. catch(Exception e){
  46. System.out.println("Exception:"+e.getMessage());
  47. e.getStackTrace();
  48. }
  49. }
  50. });
  51. }
  52.  
  53. public static void main(String[] args) {
  54. new Trying();
  55. }
Add Comment
Please, Sign In to add comment