简体   繁体   English

登录表单错误+ JDBC CONNECTIVITY(JAVA-MYSQL)

[英]a login form error + JDBC CONNECTIVITY(JAVA-MYSQL)

I have a project with JDBC connectivity (JAVA-MYSQL) which has a login up form. 我有一个具有JDBC连接(JAVA-MYSQL)的项目,该项目具有登录表单。 But the coding doesn't work.(meaning it always shows "WRONG PASSWORD" though I'm sure its the right one). 但是编码不起作用。(这意味着它始终显示“ WRONG PASSWORD”(错误密码),尽管我确定它是正确的)。

Please find the error. 请找到错误。 (cause it shows none). (因为它没有显示)。

 private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { String S; String email = jTextField3.getText(); try { Class.forName("java.sql.Driver"); Connection con = DriverManager.getConnection("jdbc:Mysql://localhost/nami", "root", "123456"); Statement st = con.createStatement(); S = "SELECT password FROM signup WHERE email =" + "'" + email + "'" + ";"; st.executeQuery(S); String pass = new String(jPasswordField2.getPassword()); if (pass.equals("S")) { jOptionPane1.showMessageDialog(null, "YOU HAVE SUCCESSFULLY LOGGED IN"); MAINPAGE at = new MAINPAGE(); jDesktopPane1.add(at); at.show(); } else { jOptionPane1.showMessageDialog(null, "WRONG PASSWORD!!"); } } catch (Exception e) { jOptionPane2.showMessageDialog(null, "Error" + e.getMessage()); } } 

pass.equals("S") means that your password should always be S in order to log in success. pass.equals("S")表示您的密码应始终为S才能成功登录。

You need to query from the database and then compare it. 您需要从数据库中查询,然后进行比较。

Result rs = st.executeQuery(S);
String queryPass = null;
if(rs.next()){
   queryPass = rs.getString("password");
}

if (pass.equals(queryPass)) {
  jOptionPane1.showMessageDialog(null, "YOU HAVE SUCCESSFULLY LOGGED IN");
  MAINPAGE at = new MAINPAGE();
  jDesktopPane1.add(at);
  at.show();

} else {
  jOptionPane1.showMessageDialog(null, "WRONG PASSWORD!!");
}

BTW,it's a bad idea to pass parameters directly into your sql,you need to use PreparedStatement instead of Statement to avoid SQL Injection 顺便说一句,将参数直接传递到sql中是一个坏主意,您需要使用PreparedStatement而不是Statement来避免SQL注入

## Try This ## ## 尝试这个 ##

 String email=request.getParameter("email");
   String pass=request.getParameter("pass");



                     Class.forName("com.mysql.jdbc.Driver");
                     Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/users_record","root","root");
                     Statement st1=con.createStatement();
                     ResultSet obj1=st1.executeQuery("select * from registration where email='" +email+ "'");

                     System.out.println(email);        
                     while(obj1.next())
                        {

                             String p2=obj1.getString(5);
                             if(p2.equals(pass))
                                 {

                                     response.sendRedirect("home1.jsp?msg=YOU HAVE SUCCESSFULLY LOGGED IN ");


                                 }
                             else
                                 {
                                       response.sendRedirect("login1.jsp?msg=Invalid password");
                                 }
                        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM