Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Create DB语句-数据库名称错误_Java_Mysql_Jdbc - Fatal编程技术网

Java Create DB语句-数据库名称错误

Java Create DB语句-数据库名称错误,java,mysql,jdbc,Java,Mysql,Jdbc,我正在尝试使用以下方法以编程方式创建DB: String DBName = "DB name 2015-02-01"; connection = DriverManager.getConnection(URL, USERNAME, PASSWORD); statement = connection.createStatement(); statement.executeUpdate("CREATE DATABASE " + DBName); 但我得到了这个错误: GRAVE: null com

我正在尝试使用以下方法以编程方式创建DB:

String DBName = "DB name 2015-02-01";
connection = DriverManager.getConnection(URL, USERNAME, PASSWORD);
statement = connection.createStatement();
statement.executeUpdate("CREATE DATABASE " + DBName);
但我得到了这个错误:

GRAVE: null
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 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 'name 2015-02-01' at line 1
我做错了什么

如何使用该
DBName
创建数据库


谢谢

我同意在名称中使用空格是一种不好的做法,但它并不像其他人所说的那样被禁止。此处记录了限制条件,仅禁止在末端留出空间

您甚至可以将表名与空格混为一谈—例如,以下工作:

fhenri@machine:~$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 4
Server version: 5.6.22 Homebrew

Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> create database `db with space`;
Query OK, 1 row affected (0,00 sec)

mysql> use `db with space`;
Database changed
mysql> create table `table with space` (column1 VARCHAR(100) NOT NULL);
Query OK, 0 rows affected (0,03 sec)

mysql> select * from `table with space`;
Empty set (0,00 sec)

mysql>
在Java中,您需要添加`字符(倒勾字符,不要与简单引号混淆')


DBName不必包含空格+是错误的方法调用。好的,谢谢,那么我如何使用
DBName
(带空格)创建DB?我不确定这是否可能,因为规范。@drgPP为什么
executeUpdate
是错误的?这就是创建db-
int Result=语句的方法
显然是从java创建db的方法。看不出有什么问题here@FredericHenri你是对的,它是有效的,API说明如下:参数
sql-一种sql数据操作语言(DML)语句,如INSERT、UPDATE或DELETE;或者是不返回任何内容的SQL语句,例如DDL语句。
我还没有读过关于DDL语句的第二部分。我想编辑我的评论。这说明我的假设是错误的。很好的例子。我试过了,但在java中不起作用。我说:
检查与您的MySQL服务器版本相对应的手册,以了解在第1行“DB name 2015-02-01”附近使用的正确语法。
@Frank,我可能稍后会检查,您使用的是“(backtick)还是“(quote),似乎您使用了quote,而backtick需要使用
String DBName = "`DB name 2015-02-01`";