Home >Database >Mysql Tutorial >How to Fix \'No Database Selected\' Error in MySQL Retrieval with JDBC?
No Database Specified: Resolving "No Database Selected" Error in MySQL Retrieval
When attempting to retrieve data from a MySQL database using JDBC, a common error encountered is "java.sql.SQLException: No database selected." This error occurs when the database name is not specified in the connection URL used to establish a connection to the database.
To address this issue, the connection URL must include the name of the database to be accessed. The correct format for a MySQL connection URL is:
jdbc:mysql://hostname:port/database_name
For example, if your database is hosted on a server with the hostname "localhost" and port "3306", and the database name is "mydb," the correct connection URL would be:
String url = "jdbc:mysql://localhost:3306/mydb";
By specifying the database name in the connection URL, you ensure that the connection is established to the correct database and that data can be retrieved successfully.
The above is the detailed content of How to Fix 'No Database Selected' Error in MySQL Retrieval with JDBC?. For more information, please follow other related articles on the PHP Chinese website!