0

Is it possible to connect to a MySQL database without specifying the username and password in Java code :

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

or is there any way to change the username and password using Java?

13
  • You seem to be asking two completely different things here: 1) Unauthenticated login; 2) password changing. Which are you interested in? Commented Feb 6, 2013 at 14:54
  • @JonSkeet For the first preference : unauthenticated login and if it is not possible then password changing. Commented Feb 6, 2013 at 14:57
  • Well is your server set up for unauthenticated login? I don't even know if that's possible, but if you haven't configured it that way, I would really hope that it would prevent you from performing unauthenticated access in JDBC. Configuring the server is the first job here. Commented Feb 6, 2013 at 14:58
  • @JonSkeet i just want that when i run my program on any other computer having a different mysql password,my program should run on it. Is there any way to do so? Commented Feb 6, 2013 at 15:01
  • I'd certainly hope not. The whole point of having a username and password is to secure the data against people who don't have the password. Do you mind if I log into your email account without a password, for example? If you need to access the data, your program should allow the password to be specified somehow (e.g. through a UI, or possibly a command line argument) Commented Feb 6, 2013 at 15:03

2 Answers 2

2

There is another method of the same name to connect anonymously (without username or password):

Connection con=DriverManager.getConnection("database url");

Source: https://docs.oracle.com/javase/7/docs/api/java/sql/DriverManager.html

Sign up to request clarification or add additional context in comments.

Comments

1

I don't know if you can setup a default user, but you can most certainly set up a user with no password.

As for the second question, assuming your connection has adequet rights in MySQL, you can most certainly set the password on any user. The most basic way to do that is to do:

update user set password=password('new_pass') where user = 'someuser';

Optionally you may want to specify the host field as well in the where clause. This needs to be ran in the msql database.

2 Comments

I think i have to make a connection with a mysql database first to be able to do this with java. But the connection will fail if I don't have the right username and password. Can I do this before making any connection with a mysql database in Java?
Java is making a "connection", so no, you don't have to manually connect to your DB first. As long as the credentials are correct, the JDBC drivers should be able to connect just fine. However, this assumes you have the correct credentials. MySQL has fairly granular security, allowing you to specify down to which host a user can connect from. I do not believe you can provide an empty user, as requested, but you can provide a null password. This link explains how to create accounts, and has an example of an account with no password: dev.mysql.com/doc/refman/5.0/en/create-user.html

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.