为什么我无法在数据库的列中检索/获取数据

时间:2018-06-08 12:18:47

标签: java mysql database

为什么我无法从数据库中的'username'和'password'列中获取或检索数据。我不知道为什么它无法从数据库中检索任何数据。我使用了语法“SELECT”,但仍无济于事。

显示:

Display that it returns null for username and password AND    this is my loggedin.jspthis is my Main.jsp以及this is my thisinvaliduser.jsp

 //GetSet.java  <-----------used as beans
 package thisPackage;

 public class GetSet {



private String uname;
private String pword;
public boolean ifvalid;



public String getUname () {

    return uname;

}

public void setUname (String myuname) {


    this.uname=myuname;
}

public String getPword () {

    return pword;

}


public void setPword (String mypword) {


this.pword=mypword;
}


public boolean isValid() {


    return ifvalid;
}

public void setisValid (boolean validity)
{

    this.ifvalid=validity;

}

}

package thisPackage;

import java.sql.Connection;
import java.sql.DriverManager;

public class konekMoko {


static Connection con;
static String url;

public static  Connection getCon () throws Exception {
    try {



        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/csdb";
        String username= "root";
        String password = "root";
        Class.forName(driver);

        //System.out.println("HAHAHAHA");
        Connection conn= DriverManager.getConnection(url, username, password);
        System.out.println("You are now Connected!!");
        return conn; // Return if it is successfully connected!



    }
    catch (Exception e)
    {
        e.printStackTrace();
        //System.out.println("Connection not Established! "+e.getMessage());
    }


    return null; // Return if unsuccessful
}

}   


/*String url = "jdbc:mysql://localhost:3306/csdb";

 Class.forName("jdbc:mysql://localhost:3306/csdb");

 try {

con = DriverManager.getConnection(url, "username", "password");

 }
  catch (Exception e) {

e.printStackTrace();

 }

 }

  catch (Exception e)
 {
  e.printStackTrace();

  }
   return con;*/

    //LoginServlet.java

 package thisPackage;

 import java.io.IOException;
 import java.io.PrintWriter;

  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;

 /**
 * Servlet implementation class LoginServlet
 */
   public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");
    PrintWriter post = response.getWriter();

    try
    {

        GetSet gsup = new GetSet();

        gsup.setUname(request.getParameter("username"));
        gsup.setPword(request.getParameter("password"));

        //System.out.println("HAHAHAHAHA");

        gsup = myDAO.login(gsup);

        if (gsup.isValid())
        {

            post.print("Welcome" + gsup);
            HttpSession mysession = request.getSession(true);
            mysession.setAttribute("thiscurrentsuser", gsup);
            mysession.setMaxInactiveInterval(120); // 2 mins stay on the page
            response.sendRedirect("loggedin.jsp"); // the user will be redirected to the loggedin page if true


        }

        else
        {

            response.sendRedirect("thisinvaliduser.jsp"); //if it is not valid it will throw you to the invalid log page

        }

    }

    catch (Throwable e){

        System.out.println(e);


    // TODO Auto-generated method stub
    doGet(request, response);
}

}
}

 //MyDAO.java


package thisPackage;

 import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
 //import java.sql.Statement;

 public class myDAO {

//static Connection con = null;
//static ResultSet rs = null;


public static GetSet login (GetSet bean) {

    Connection con = null;
    PreparedStatement ps = null;
    ResultSet rs =null;
    String uname = bean.getUname();
    String pword = bean.getPword();

    String searchQuery = "select *from csusers where username=? AND password=?";
    //String searchQuery = "select username,password *from csusers";




    System.out.println("Your username is: " + uname);
    System.out.println("Your password is: " + pword);
    //System.out.println("Your query is: " + searchQuery);

try {

    con = konekMoko.getCon();
    ps = con.prepareStatement(searchQuery);
    rs = ps.executeQuery();

    ps.setString(1, uname);
    ps.setString(2, pword);
    boolean tochek =rs.next();

    if (!tochek)                                                // checks if username already existing
    {

        System.out.println("Username does not exists!");
        bean.setisValid(false);

    }

    else if (tochek)                                            //if already existing
    {

        System.out.println("Welcome" + uname);
        bean.setUname(uname);
        bean.setPword(pword);
        bean.setisValid(true);

    }
    rs.close();
    ps.close();
    con.close();

}

catch (Exception e) {


}
return bean;

}


/*public static Connection getCon() throws Exception {
    try {

        String driver = "com.mysql.jdbc.Driver";
        String url = "jdbc:mysql://localhost:3306/mydb";
        String username= "root";
        String password = "root";
        Class.forName(driver);

        Connection conn= DriverManager.getConnection(url, username, password);
        System.out.println("You are now Connected!!");
        return conn; // Return if it is successfully connected!



    }
    catch (Exception e)
    {

        System.out.println("Connection not Established!"+e.getMessage());
    }


    return null; // Return if unsuccessful
}
*/


 }

1 个答案:

答案 0 :(得分:2)

在为查询设置参数

之前,您正在执行查询
con = konekMoko.getCon();
ps = con.prepareStatement(searchQuery);
rs = ps.executeQuery(); <-- This is in the wrong order

ps.setString(1, uname);
ps.setString(2, pword);

将您的代码更改为

con = konekMoko.getCon();
ps = con.prepareStatement(searchQuery);
ps.setString(1, uname);
ps.setString(2, pword);

rs = ps.executeQuery();