No database selected Query: Solve the problem that the correct data cannot be queried using question mark placeholders in database queries

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
Insert picture description here

2. The following is the content of my MySQL database:
Insert picture description here

Insert picture description here

Operation process:

3. When I open the server, inputting the user name and password
Insert picture description here
should theoretically show that the login is successful, the account password is completely consistent with the database,
but the result is :
Insert picture description here


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
Insert picture description here
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 Insert picture description here
tom...tom123is the user input result I printed. This is correct, indicating that the user input to the server is no problem.
Insert picture description here

nullI printed the object after the database query, which nullmeans that an error occurred during the query.

Here comes the point

Insert picture description here

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

Insert picture description here

Start the Tomcat server again and enter the same content.

Insert picture description here

  • Jump page
    Insert picture description here
  • Back to idea:

The two print results in the test code have reached expectations, and the query object has also come out.

Insert picture description here
Insert picture description here

  • 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 数据库名
Insert picture description here
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
      Insert picture description here
    • check sentence
      Insert picture description here

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~

Guess you like

Origin blog.csdn.net/weixin_43876778/article/details/114143918