jQuery in Action, 2nd edition*
The moose likes JDBC 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


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Detect mysql invalid username/password" Watch "Detect mysql invalid username/password" New topic
Author

Detect mysql invalid username/password

Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
Hi,

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.

Thanks,

Lou

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 {
super.init(config);
try {
Class.forName("org.gjt.mm.mysql.Driver");
Properties p = new Properties();
p.put("user", "root");
p.put("anonymous","");
con = DriverManager.getConnection(DB_URL, p); } catch (SQLException sqlex) {
sqlex.printStackTrace();
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
}
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2497
    
    8

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
youtube channel
Lou Pelagalli
Ranch Hand

Joined: Nov 11, 2003
Posts: 150
    
    1
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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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: http://aspose.com/file-tools
 
subject: Detect mysql invalid username/password