简体   繁体   English

Java JDBC MySQL查询失败

[英]Java JDBC MySQL query failing

I'm working on querying a local MySQL database using a Java Spring application with JDBC. 我正在使用带有JDBC的Java Spring应用程序查询本地MySQL数据库。 My application configures a DataSource object using the SimpleDriverDataSource() constructor. 我的应用程序使用SimpleDriverDataSource()构造函数配置DataSource对象。 Then it makes a JDBCTemplate object from that DataSource . 然后它从该DataSource一个JDBCTemplate对象。

It's supposed to be running queries on the MySQL database through that template object, but the queries are failing for some unknown reason! 它应该是通过该模板对象在MySQL数据库上运行查询,但查询因某些未知原因而失败! I think it may have something to do with my DataSource . 我认为这可能与我的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. 在调试中, dbsrc似乎已定义。 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 这是Eclipse在定义之后所说的内容的剪辑: dbsrc定义

I'm thinking something is not right about connectionProperties being null . 我在想有些关于connectionPropertiesnull东西是不对的。

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). 当我逐步执行该语句的执行时,它进入jdbcTemplate.query()方法并且似乎在某些我看不到的步骤中抛出异常(Eclipse告诉我在项目中找不到源文件)。

Any help would be greatly appreciated and I'm happy to provide any other necessary information. 任何帮助将不胜感激,我很乐意提供任何其他必要的信息。 Thank you! 谢谢!

Thank you everybody for your help! 谢谢大家的帮助! As it turns out, the issue was unrelated to the DataSource or JDBCTemplate objects. 事实证明,该问题与DataSource或JDBCTemplate对象无关。

For the project I was working on, I was given a pre-made pom.xml configuration file. 对于我正在进行的项目,我获得了一个预先制作的pom.xml配置文件。 This file contained this MySQL version property tag: 该文件包含此MySQL版本属性标记:

<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: 为了使JDBCTemplate对象正确执行查询,我所要做的就是将该版本号更改为我正在使用的MySQL版本:

<properties>
    <mysql.version>8.0.15</mysql.version>
</properties>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM