Project scene:
There is data in the database but cannot be queried. The error content is:java.sql.SQLException: No database selected Query
Problem Description:
Environment description:
1. The following is the code of my Tomcat server login program (the environment is IDEA), I use the? Placeholder to query the data
2. The following is the content of my MySQL database:
Operation process:
3. When I open the server, inputting the user name and password
should theoretically show that the login is successful, the account password is completely consistent with the database,
but the result is :
Cause Analysis:
接下来进入了漫漫排除之路~
At first I suspected that the statement was wrong, so I integrated the database in the idea, and wrote the SQL statement in the console
4. I used the console to query the results and the
results were queried in the database .
After excluding the reason for writing the SQL statement, I carefully checked the output of the Tomcat server
5. View the server output
tom...tom123
is the user input result I printed. This is correct, indicating that the user input to the server is no problem.
null
I printed the object after the database query, which null
means that an error occurred during the query.
Here comes the point
At this time I noticed an error:
No database selected Query
Means that no database was selected for query? ? ?
I obviously chose the users form, and I seriously doubt it is because I used the question mark placeholder! !
After checking some information, I wondered if it was because my database did not have a clear choice? Let idea not know which database to query ?
solution:
So I carefully added the database name in front of the users table. Written asweb01.users
Start the Tomcat server again and enter the same content.
- Jump page
- Back to idea:
The two print results in the test code have reached expectations, and the query object has also come out.
- It shows that the query was successful~
summary:
- This error doesn't seem to happen to everyone when querying database data, but since I have it, let me record it.
- It may be that the query in IDEA will cause such a problem that the database cannot be found? Or the database is not connected and configured. However , as long as the " database name. table name " is used, there will be no problems that can not be queried ! ~
Update :
emm, I found a problem, the configuration file C3P0 connection pool, the concrete does not write 数据库名
so will lead to burst before java.sql.SQLException: No database selected Query
, because I did not choose a specific database Well , it is embarrassing ~
- After I added the specific database name in the configuration file, the problem no longer existed.
- Configuration file
- check sentence
- Configuration file
However, this means that we can not write the database that needs to be queried in the configuration file, and we can write it clearly in the sql statement, which is also possible. Although tossing a lot, when querying a certain database like this, there are two ways to write the statement~