Resolve Unknown database in JDBC error with Java-MySQL?


This type of error occurs if you select any database that does not exist in MySQL. Let us first display the error of unknown database in JDBC.
The Java code is as follows. Here, we have set the database as ‘onlinebookstore’, which does not exist:
import java.sql.Connection;
import java.sql.DriverManager;
public class UnknownDatabaseDemo {
   public static void main(String[] args) {
      String JdbcURL = "jdbc:mysql://localhost:3306/onlinebookstore?useSSL=false";
      String Username = "root";
      String password = "123456";
      Connection con = null;
      try {
         con = DriverManager.getConnection(JdbcURL, Username, password);
         System.out.println("Connected to MySQL database");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
The following is the output displaying the same error
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'onlinebookstore'
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
   at com.mysql.jdbc.Util.getInstance(Util.java:408)
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3978)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3914)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:871)
   at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1714)
   at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1224)
   at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2199)
   at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2230)
   at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2025)
   at com.mysql.jdbc.ConnectionImpl.(ConnectionImpl.java:778)
   at com.mysql.jdbc.JDBC4Connection.(JDBC4Connection.java:47)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
   at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
   at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
   at java.lang.reflect.Constructor.newInstance(Unknown Source)
   at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
   at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:386)
   at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:330)
   at java.sql.DriverManager.getConnection(Unknown Source)
   at java.sql.DriverManager.getConnection(Unknown Source)
   at UnknowDatabaseDemo.main(UnknowDatabaseDemo.java:15)
The screenshot of the error is as follows
To remove this type of error, just go to MySQL command line and show all database names and use the chosen one from there i.e. the database which really exist.
Here, we have lots of database and I am going to choose database ‘hb_student_tracker’. The following is the list of all database names, we got using SHOW DATABASES command:
Here is the JDBC code that connects the database “hb_student_tracker”. The code is as follows:
import java.sql.Connection;
import java.sql.DriverManager;
public class UnknownDatabaseDemo {
   public static void main(String[] args) {
      String JdbcURL = "jdbc:mysql://localhost:3306/hb_student_tracker?useSSL=false";
      String Username = "root";
      String password = "123456";
      Connection con = null;
      try {
         con = DriverManager.getConnection(JdbcURL, Username, password);
         System.out.println("Connected to MySQL database");
      } catch (Exception e) {
         e.printStackTrace();
      }
   }
}
The screenshot is as follows displaying the output: