[CONJ-526] Wrong error message when calling ParameterMetaData getParameterCount() with incorrect SQL in PreparedStatement Created: 2017-09-14  Updated: 2017-09-20  Resolved: 2017-09-18

Status: Closed
Project: MariaDB Connector/J
Component/s: Other
Affects Version/s: 2.1.1
Fix Version/s: 1.6.5, 2.1.2

Type: Bug Priority: Minor
Reporter: Rafael Lopez Fernández Assignee: Diego Dupin
Resolution: Fixed Votes: 0
Labels: None

Attachments: Java Source File ClientStandalone.java     Text File patch.patch    

 Description   

When calling getParameterCount() from the ParameterMetaData of a PreparedStatement with incorrect SQL, it fails with "Parameter metadata not available for these statement" instead of "You have an error in your SQL syntax; ..."

For example:
Executing query:

S1ELECT * from duala WHERE a = ?

fails with

java.sql.SQLException: Parameter metadata not available for these statement

Corrected version:

S1ELECT * from duala WHERE a = ?

fails with

java.sql.SQLSyntaxErrorException: (conn=20657507) Error preparing query: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'S1ELECT * from duala WHERE a = ?' at line 1

Attached example:

	PreparedStatement pstmt = connection.prepareStatement(sql);
	final ParameterMetaData pmd = pstmt.getParameterMetaData();
	//wrong error message:
	final int count = pmd.getParameterCount();

I think one possible workaround is to change setParametersData() method in
class org.mariadb.jdbc.MariaDbPreparedStatementClient to capture SQLSyntaxErrorException and to rethrow it

private void setParametersData() throws SQLSyntaxErrorException {
	try (MariaDbPreparedStatementServer ssps = new MariaDbPreparedStatementServer(connection, this.sqlQuery,
			ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY, Statement.NO_GENERATED_KEYS)) {
		resultSetMetaData = ssps.getMetaData();
		parameterMetaData = ssps.getParameterMetaData();
	} catch (SQLSyntaxErrorException sqlsse) {
		// syntax error thrown
		throw sqlsse;
	} catch (SQLException sqle) {
		//if statement cannot be prepared
		parameterMetaData = new MariaDbParameterMetaData(null);
	}
}



 Comments   
Comment by Diego Dupin [ 2017-09-18 ]

Thanks for reporting that.
Clearly error message has to be reported in case of bad SQL syntax.

This will be corrected in 2.2.0 version and 1.6.5 version

(actually available in snapshot :

<repositories>
    <repository>
        <id>sonatype-nexus-snapshots</id>
        <name>Sonatype Nexus Snapshots</name>
        <url>https://oss.sonatype.org/content/repositories/snapshots</url>
    </repository>
</repositories>
 
<dependencies>
    <dependency>
        <groupId>org.mariadb.jdbc</groupId>
        <artifactId>mariadb-java-client</artifactId>
        <version>2.2.0-SNAPSHOT</version>
    </dependency>
</dependencies>

Generated at Thu Feb 08 03:16:20 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.