Bug #94585 getTableName() returns null for a query having count(*) with JDBC driver v8.0.12
Submitted: 7 Mar 2019 7:39 Modified: 24 May 2019 22:01
Reporter: kriti suwalka Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / J Severity:S3 (Non-critical)
Version:8.0.12, 8.0.15 OS:Any
Assigned to: CPU Architecture:Any
Tags: regression

[7 Mar 2019 7:39] kriti suwalka
Description:
When a getTableName() is called on the metadata result set obtained from preparing an SQL query having count(*), it returns null which is change in behavior from JDBC driver version 5.1.40. 5.1.40 driver returns an empty string instead of null. 

We found that it returns null with 5.1.44 driver as well but we couldn't find any documentation which states that it's a change is behavior.

How to repeat:
Run the following standalone code to reproduce the issue

package jdbc_sample_latest;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.Properties;

public class GetMedataForQuery {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//Replace <host>, <portNumber> and <DBName> with appropriate values.
		String jdbcConnectionURL = "jdbc:mysql://<host>:<portNumber>/<DBName>";

		Properties prop = new Properties();
		
		//Replace <userName> and <password> with appropriate values.
		String userName = "<userName>";
		String password = "<password>";
		
		prop.put("user", userName);
		prop.put("password", password);
		
		Connection con = null;
		try{
			
		    con = DriverManager.getConnection(jdbcConnectionURL, prop);
		    System.out.println("connection is successful");
		    System.out.println("Driver version is : " + con.getMetaData().getDriverVersion());
		    
		    ResultSetMetaData rsMeta;
		    
		    //Replace <tableName> with appropriate value.
		    String query = "select count(*) from <tableName>";
		    
	        PreparedStatement pstmt = con.prepareStatement(query);
	        rsMeta = pstmt.getMetaData();
	        if (rsMeta != null) {
	        	for (int i = 1; i <= rsMeta.getColumnCount(); i++) {
	        		System.out.println("Table Name is : " + rsMeta.getTableName(i));
	            }
	        }
		}catch(SQLException e) {
			e.printStackTrace();
		}
	}
}

Output with 8.0.12 driver for our case
----------------------------------------------------------------------------
connection is successful
Driver version is : mysql-connector-java-8.0.12 (Revision: 24766725dc6e017025532146d94c6e6c488fb8f1)
Table Name is : null
----------------------------------------------------------------------------

Output with 5.1.40 driver for our case
----------------------------------------------------------------------------
connection is successful
Driver version is : mysql-connector-java-5.1.40 ( Revision: 402933ef52cad9aa82624e80acbea46e3a701ce6 )
Table Name is : 
----------------------------------------------------------------------------
[7 Mar 2019 8:50] MySQL Verification Team
Hello kriti,

Thank you for the report.

regards,
Umesh
[7 Mar 2019 12:38] kriti suwalka
Is there any way to go back to old behavior ?
[11 Mar 2019 13:45] kriti suwalka
Issue is seen with almost all aggregate functions, to name a few min,max,concatenation,cast etc.
[24 May 2019 22:01] Daniel So
Posted by developer:
 
Added the following entry to the Connector/J 8.0.17 and 5.1.48 changelog: 

"ResultSetMetaData.getTableName() returned null when no applicable results could be returned for a column. However, the JDBC documentation specified an empty string to be returned in that case. This fix makes the method behave as documented. The same correction has been made for getCatalogName() and getSchemaName()."