Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Detect mysql invalid username/password

 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2584
11
C++ Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Lou Pelagalli
Ranch Hand
Posts: 150
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20758
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic