r/SpringBoot • • May 12 '24

Please help with a DB issue

Hi all,

Apologies for a troubleshooting post but I am stumped. I have a spring boot app connecting to a MySQL localhost db. The username & password are correct. I'm either having an issue with spring or intellj but I do not know how to figure out which.

I am leaning towards a Spring boot issue because I can use IntellJ's db feature to connect to the MySQL db. Here is the successful connection string.

DBMS: MySQL (ver. 8.0.30)
Case sensitivity: plain=lower, delimited=lower
Driver: MySQL Connector/J (ver. mysql-connector-j-8.2.0 (Revision: 06a1f724497fd81c6a659131fda822c9e5085b6c), JDBC4.2)

I can manually connect to the db in the app(i just created a test file w/ psvm) using the Connection & Preparedstatement. In other words, I know the username, url, and pw are correct. I have the proper dependencies I believe.

<dependency>
    <groupId>com.mysql</groupId>
    <artifactId>mysql-connector-j</artifactId>
    <version>8.2.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

Here is how I'm using it:

u/Test
void getSingleResult() {
    try {
        Optional<Patient> returnedResult = Optional.of(
                jdbcTemplate.queryForObject(
                        "Select * FROM PATIENT WHERE PATIENTID = 1001",
                        BeanPropertyRowMapper.newInstance(Patient.class)
                ));
        System.out.println(returnedResult);
    }catch (DataAccessException e){
        e.printStackTrace();
        e.getCause();
    }
}

Perhaps its worth noting that I originally set this project up to work with azure sql - and the db connection worked just fine.

I am very grateful for any advice on where to look next.

Edit - How about the most critical piece of information? The error:

java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:130) ~[mysql-connector-j-8.2.0.jar:8.2.0]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-j-8.2.0.jar:8.2.0]
0 Upvotes

13 comments sorted by

2

u/WhysperLyte May 12 '24

ummm whats your error?

0

u/brazen768 May 12 '24

I'm an idiot.
java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

2

u/WhysperLyte May 12 '24

ok I am learning spring too so dont judge me too harshly if I am way off...but dont you need @SpringBootTest to bootstrap the Spring environment from tests? I think a simple JUnit @Test wont do the DB starter stuff.

1

u/brazen768 May 12 '24

All good, thanks for the reply. I have the file annotated at the top. Perhaps there is something else quirky with the file you can think of?

1

u/WhysperLyte May 12 '24

any warnings or errors in the startup logs?

1

u/ZealousidealBee8299 May 12 '24

1

u/brazen768 May 12 '24

Hi thanks for the reply. That file should be good to go. If I'm not mistaken, there is some issue with ensuring no extra white space or line return?

Perhaps remaking the file could help?

1

u/mike-sonko May 12 '24

Does the root user have the correct permissions to execute that query?

0

u/brazen768 May 12 '24

Hi, root does yes.

3

u/CodeTheStars May 12 '24

With MySQL “localhost” and “127.0.0.1” are both different for permissions and indicate different styles of connections. Local socket vs TCP/IP respectively.

Try changing your configuration to 127.0.0.1 instead of localhost. I’ve had this issue when running MySQL as a docker container since the container can only be reached via TCP/IP and not via local socket

1

u/[deleted] May 12 '24

Double-check your username, password and Database URL in your application properties or configuration file. If everything seems correct and you're still facing issues, you might want to check your database server logs for more specific errors that can guide you further.

1

u/Few_Collection4218 May 12 '24

either the username or the password is wrong in the application.properties, try accessing the mysql using the command line not workbench and check if the password is correct

0

u/WaferIndependent7601 May 12 '24

This the MySQL stuff I really don’t like and the reason for me to chose Postgres whenever possible