[Solved] JDBC : java.sql.SQLSyntaxErrorException: Unknown database ‘database_name’

java.sql.SQLSyntaxErrorException is unchecked runtime exception which is sub class of SQLException. Herejava.sql.SQLSyntaxErrorException: Unknown database database_name”  exception occurred when connecting to database schema by JDBC. Please check below for more detail.

Sample Code

try
{
Class.forName("com.mysql.jdbc.Driver");
connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/FacingIssuesOnITDB", "root", "facingissuesonit");
}
catch(ClassNotFoundException ex)
{
ex.printStackTrace();
}

Output


java.sql.SQLSyntaxErrorException: Unknown database 'facingissuesonitdb'
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:118)
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.ConnectionImpl.createNewIO(ConnectionImpl.java:862)
at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:444)
at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:230)
at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:226)
at java.sql.DriverManager.getConnection(DriverManager.java:664)
at java.sql.DriverManager.getConnection(DriverManager.java:247)
at com.fioit.examples.jdbc.connection.MyConnection.getConnectionMain(MyConnection.java:59)
at com.fioit.examples.jdbc.crud.CRUDOperation.main(CRUDOperation.java:20)

Issue

Above issue “java.sql.SQLSyntaxErrorException: Unknown database database_name” occurred because no schema exist in MySQL database with name facingissuesonitdb.

Solution

In JDBC always handle java.sql.SQLException in your connection code also because if any issue happen for java.sql.SQLSyntaxErrorException can catch by SQLException block.

More 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.