0

I'm working on querying a local MySQL database using a Java Spring application with JDBC. My application configures a DataSource object using the SimpleDriverDataSource() constructor. Then it makes a JDBCTemplate object from that DataSource.

It's supposed to be running queries on the MySQL database through that template object, but the queries are failing for some unknown reason! I think it may have something to do with my DataSource.

Here are the relevant bits of code. First, I configure a Data Source object like so:

public DataSource getDataSource() {
      DataSource dbsrc = null;
      try {
          dbsrc = new SimpleDriverDataSource(new com.mysql.jdbc.Driver(), "jdbc:mysql://localhost/test?useSSL=false", "root", "");
      } catch (SQLException e) {
          e.printStackTrace();
      }
      return dbsrc;
    }

In debugging, the dbsrc appears to get defined. Whether or not it's correctly defined is another story. Here's a clip of what Eclipse says it is after it's definition: dbsrc definition

I'm thinking something is not right about connectionProperties being null.

Here is an example of a query that is failing:

final List<String> methods = jdbcTemplate.query(methodsQuery, new RowMapper<String>() {
          @Override
          public String mapRow(ResultSet rs, int rowNum) throws SQLException {
            return rs.getString("PLANETDISCMETH");
          }
        });

When I step through that statement's execution, it enters the jdbcTemplate.query() method and appears to throw an exception at some step that I cannot see (Eclipse tells me the source file cannot be found in the project).

Any help would be greatly appreciated and I'm happy to provide any other necessary information. Thank you!

Finley
  • 392
  • 1
  • 8
  • 18
  • 1
    Can you post a stacktrace of your exception? Are you using spring's SimpleDriverDataSource? – Daniele May 09 '19 at 20:45
  • @Daniele Thanks for the response! It doesn't actually show a stack trace for some reason. In fact, it may not even be throwing an actual exception. All I know is the execution gets stuck somewhere inside that `query()` call since the debugger pauses inside that call. And yes, I am using SimpleDriverDataSource to define my DataSource object as shown in the first code snippet. That DataSource is then used to construct the JDBCTemplate object that I'm running the queries through. – Finley May 09 '19 at 20:58
  • If you don't have a stacktrace, how do you know that your query is failing. Without exception information or a clear description of your problem, we really can't help you. – Mark Rotteveel May 10 '19 at 07:42
  • Maybe the driver library is throwing an exception, and that exception is then caught by the spring library. You don't want to suspend execution in that case. If you are using Eclipse, see this link to avoid suspending: https://stackoverflow.com/questions/3685294/how-to-configure-eclipse-to-skip-exceptions-in-debug-mode . If then *spring* explodes, it should report the cause – Daniele May 10 '19 at 09:06
  • @MarkRotteveel Thanks for your response! I know it's failing because the code after the query call is never executed, neither is the code inside the RowMapper. I think execution stops inside the query call because of some exception being thrown. I can't see a stack trace because exceptions are handled inside the JDBCTemplate object. I do wish I could see why it's failing! But I have not found a way. – Finley May 10 '19 at 16:03
  • If an exception happens in JDBCTemplate, it will wrap that exception and throw it. If it was swallowed, your own code would have continued. – Mark Rotteveel May 10 '19 at 16:06

1 Answers1

0

Thank you everybody for your help! As it turns out, the issue was unrelated to the DataSource or JDBCTemplate objects.

For the project I was working on, I was given a pre-made pom.xml configuration file. This file contained this MySQL version property tag:

<properties>
    <mysql.version>5.0.13</mysql.version>
</properties>

To make the JDBCTemplate object execute queries properly, all I had to do was change that version number to the version of MySQL that I was using:

<properties>
    <mysql.version>8.0.15</mysql.version>
</properties>
Finley
  • 392
  • 1
  • 8
  • 18