The moose likes JDBC and Relational Databases and the fly likes Detect mysql invalid username/password Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC and Relational Databases
Bookmark "Detect mysql invalid username/password" Watch "Detect mysql invalid username/password" New topic

Detect mysql invalid username/password

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150

Is it possible to detect when an invalid username/password is used when trying to establish a connection to mysql?

The code below just returns a null pointer exception when I try to connect.



public class DatabaseAccess extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
private Connection con;
private static final String DB_URL = "jdbc:mysql://localhost/eMotherEarth";

// Get connection.
public void init(ServletConfig config) throws ServletException {
try {
Properties p = new Properties();
p.put("user", "root");
con = DriverManager.getConnection(DB_URL, p); } catch (SQLException sqlex) {
} catch (ClassNotFoundException cnfe) {
Jan Cumps

Joined: Dec 20, 2006
Posts: 2565

In this block of code, you currently print the error message:
catch (SQLException sqlex) {}

You could find out in this block if the error returned by your database tells that the user/password is invalid.

OCUP UML fundamental and ITIL foundation
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
Thank you Jan.

Unfortunately java.lang.NullPointerException is the throwable causing the problem so SQLException is not caught.

When I catch java.lang.NullPointerException and print it it does not provide any information about the user or password.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

You could find out more information (such as what line of code is throwing the exception) by looking at the stack trace, which tells you such things. And of course the NullPointerException doesn't tell you anything about your password; it just tells you that you have a programming error.

I also don't see where you are even passing a password to MySQL.
I agree. Here's the link:
subject: Detect mysql invalid username/password
It's not a secret anymore!