多种方法解决java.sql.SQLSyntaxErrorException: Unknown database ‘xxx‘的错误

文章目录

  • 1. 复现错误
  • 2. 分析错误
  • 3. 解决错误
  • 4. 解决错误的其他方法

1. 复现错误

今天尝试创建test2的数据库,却报出如下错误:

多种方法解决java.sql.SQLSyntaxErrorException: Unknown database ‘xxx‘的错误_第1张图片

java.sql.SQLSyntaxErrorException: Unknown database 'test2'
	at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)
	at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:833)
	at com.mysql.cj.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:453)
	at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)
	at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:198)
	at java.sql.DriverManager.getConnection(DriverManager.java:664)
	at java.sql.DriverManager.getConnection(DriverManager.java:247)
	at com.test.cloud.utils.DatasourceUtil.getByDriverManager(DatasourceUtil.java:192)
	at com.test.cloud.utils.DatasourceUtil.getConnection(DatasourceUtil.java:135)
	at com.test.cloud.utils.DatasourceUtil.createDb(DatasourceUtil.java:59)
	at com.test.cloud.controller.DatasourceController.createDatasource(DatasourceController.java:70)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:197)
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:141)
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:106)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:894)
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1063)
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
	......

Unknown database 'test2'

为什么报出未知数据库test2的错误呢?

接下来,我便详细分析此错误。

2. 分析错误

正赶上最近ChatGPT很火,于是借助ChatGPT回复我的问题:

多种方法解决java.sql.SQLSyntaxErrorException: Unknown database ‘xxx‘的错误_第2张图片

java.sql.SQLSyntaxErrorException: Unknown database ‘xxx’

This error suggests that the database specified does not exist. You will need to check the database name, spelling and case sensitivity. If the database does not exist you may need to create it. and case sensitivity. If the database does not exist you may need to create it.

ChatGPT给出英文的回复,我们不妨翻译成中文,如下所示:

此错误表明指定的数据库不存在。您需要检查数据库名称、拼写和区分大小写。如果数据库不存在,则可能需要创建它。和区分大小写。如果数据库不存在,则可能需要创建它。

ChatGPT的回复来看,有以下2种情况导致:

  1. 数据库不存在

  2. 数据库设置了大小写

然而,我的目的是创建数据库,数据库自然不存在,况且我没有区分大小写。

看来,ChatGPT没有解决我的问题,我只能通过断点的方式来解决了,如下图所示:

多种方法解决java.sql.SQLSyntaxErrorException: Unknown database ‘xxx‘的错误_第3张图片

通过断点可以清楚地看到,连接数据库的url有问题,如下所示:

jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=GMT%2B8&useSSL=false

我还未创建test2数据库,故3306后不能加上/test2

3. 解决错误

由于公司源码不能对外暴露,这是技术人员最基本的职业素养,因而,我写如下测试代码:

@Test
public void testJdbc() {
  String url = "jdbc:mysql://127.0.0.1:3306?serverTimezone=GMT%2B8&useSSL=false";
  String username = "root";
  String password = "123456";
  Connection connection = null;
  PreparedStatement preparedStatement = null;
  try {
    connection = DriverManager.getConnection(url, username, password);
    preparedStatement = connection.prepareStatement("CREATE DATABASE test2");
    int i = preparedStatement.executeUpdate();
    if (i > 0) {
      System.out.println("创建成功");
    }
  } catch (SQLException e) {
    System.out.println("创建失败");
    e.printStackTrace();
  } finally {
    //不论什么时候,都要关闭,否则会占用资源
    try {
      if (preparedStatement != null) {
        preparedStatement.close();
      }
      if (connection != null) {
        connection.close();
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }
}

执行结果如下图所示:

多种方法解决java.sql.SQLSyntaxErrorException: Unknown database ‘xxx‘的错误_第4张图片

4. 解决错误的其他方法

如果我的解决错误方法,无法解决你的问题,可以尝试用如下方法解决你的错误。

  1. 检查配置文件中的数据库名是否错误

比如,我的数据库名为test,而我不小心写成了test1,如下所示:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/test1?serverTimezone=GMT%2B8&useSSL=false

这样,自然就报出Unknown database ‘test1‘

  1. 检查数据库是否设置了大小写

比如,我的数据库名为TEST,而我不小心写成了test,如下所示:

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: 123456
    url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useSSL=false

这样,自然就报出Unknown database ‘test‘

假设,你设置了数据库的大小写,可以参考这篇博文解决:Windows和linux下分别区分mysql大小写的问题

总之一句话,你的代码无法找到对应的数据库,请你检查你代码哪里出现了问题。

  1. 检查数据库是否存在

若你的数据库不存在,而你配置文件中添加了数据库,这样自然就报错。

你可能感兴趣的:(java,mysql,java,数据库,sql,mysql,后端)