我尝试创建数据库和表。 如果我在两个语句/连接中执行它,它可以正常工作:
String createDb = "CREATE DATABASE IF NOT EXISTS XYZ; ";
statement.executeUpdate(createDb);
然后
String createTable = "CREATE TABLE IF NOT EXISTS XYZ.Teachers ( idTeacher SMALLINT(3) );";
// new connection
statement.executeUpdate(createTable);
但是,如果我尝试将两个命令放在一起创建一个带有单个语句/连接的数据库和表,我会得到以下异常:
String createDbAndTable = "CREATE DATABASE IF NOT EXISTS XYZ; " +
"CREATE TABLE IF NOT EXISTS XYZ.Teachers ( idTeacher SMALLINT(3) );";
statement.executeUpdate(createTable);
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 'CREATE TABLE IF NOT EXISTS XYZ.Teachers ( idTeacher SMALLINT(3) )' at
line 1
我想,不可能像这样放两个sql命令。无论如何,还有其他方法可以使用单个语句/连接吗?