[Solved] JDBC: java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near…

java.sql.SQLSyntaxErrorException is sub class of java.sql.SQLException which occurs in JDBC  while executing any SQL query not following syntax or any typo mistake with keywords. Here trying to drop database schema.

Constructors

  • SQLSyntaxErrorException() : Construct a SQLSyntaxErrorException Object.
  • SQLSyntaxErrorException(String reason) : Construct a SQLSyntaxErrorException Object with a given reason.
  • SQLSyntaxErrorException(String reason, String SQLState) : Construct a SQLSyntaxErrorException Object.,  with a given reason and SQLState.
  • SQLSyntaxErrorException(String reason, String SQLState, int vendorCode) : Construct a SQLSyntaxErrorException Object.with a given reason, SQLState and vendorCode.
  • SQLSyntaxErrorException(String reason, String SQLState, int vendorCode, Throwable cause) : Construct a SQLSyntaxErrorException Object .with a given reason, SQLState  vendorCode and cause..
  • SQLSyntaxErrorException(String reason, String SQLState, ,Throwable cause) : Construct a SQLSyntaxErrorException Object .with a given reason, SQLState and cause..
  • SQLSyntaxErrorException(tring reason, Throwable cause) : Construct a SQLSyntaxErrorException Object with reason and cause.
  • SQLSyntaxErrorException( Throwable cause) : Construct a SQLSyntaxErrorException Object with cause.

 

Sample Code

 try
{
Class.forName("com.mysql.jdbc.Driver");
Connection	connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/", "root", "facingissuesonit");
Statement smt = conn.createStatement();
System.out.println("Drop Database ....");
smt.executeUpdate("drop database ifexist FacingIssuesOnITDB");
System.out.println("Database drop successfully ....");
		}
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
catch (SQLException ex) {
ex.printStackTrace();
}
<span style="display:inline !important;float:none;background-color:transparent;color:#3d596d;cursor:text;font-family:'Noto Serif', Georgia, 'Times New Roman', Times, serif;font-size:16px;font-style:normal;font-variant:normal;font-weight:400;letter-spacing:normal;line-height:19.2px;orphans:2;text-align:left;text-decoration:none;text-indent:0;text-transform:none;white-space:normal;word-spacing:0;">catch (SQLSyntaxErrorException ex) { ex.printStackTrace(); } </span>

 

Output Message


java.sql.SQLException: Can't drop database 'facingissuesonitdb'; database doesn't exist
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:127)
    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:95)
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
    at com.mysql.cj.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1393)
    at com.mysql.cj.jdbc.StatementImpl.executeLargeUpdate(StatementImpl.java:2353)
    at com.mysql.cj.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1303)
    at com.fioit.jdbc.examples.schemasetup.DatabaseOperationTest.dropDatabase(DatabaseOperationTest.java:59)
    at com.fioit.jdbc.examples.schemasetup.DatabaseOperationTest.main(DatabaseOperationTest.java:19)

Issue

java.sql.SQLSyntaxErrorException occurs in JDBC SQL query because of typo mistake there should space between two words ifexist that what throwing this exception. 

Solutions

Use correct query as below for dropping database.

for Example

Drop Database:

drop database if exists FacingIssuesOnITDB

Issues Solution

For more other JAVA/JDBC issues solution follow link JDBC Issues and Solutions.

Advertisements

This site uses Akismet to reduce spam. Learn how your comment data is processed.