wood burning stoves*
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
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: 2491

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
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: 18541

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.
Consider Paul's rocket mass heater.
subject: Detect mysql invalid username/password
Similar Threads
Network could not establish connection in jdbc
servlet example without using MVC pattern
Lost properties
The Network Adapter could not establish the connection in jdbc connection
Connection Pooling example appears to create more than one pool