JDBC MySQL连接,无需指定密码

时间:2022-01-30 03:49:44

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

是否可以连接到MySQL数据库而无需在Java代码中指定用户名和密码:

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

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

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

或者有没有办法使用Java更改用户名和密码?

2 个解决方案

#1


0  

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:

至于第二个问题,假设你的连接在MySQL中拥有很多权限,你肯定可以在任何用户上设置密码。最基本的方法是:

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.

您可以选择在where子句中指定主机字段。这需要在msql数据库中运行。

#2


0  

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

#1


0  

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:

至于第二个问题,假设你的连接在MySQL中拥有很多权限,你肯定可以在任何用户上设置密码。最基本的方法是:

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.

您可以选择在where子句中指定主机字段。这需要在msql数据库中运行。

#2


0  

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