大家好! 我正在尝试使用java创建数据库。 这是代码
public void createDatabase(String user, String password) {
String query = "CREATE DATABASE IF NOT EXISTS Contacts;use Contacts;CREATE TABLE IF NOT EXISTS info(first_name TEXT,surname TEXT,login TEXT,email TEXT,phone_number INT NOT NULL primary key)";
Connection connection = createConnection(user, password);
try {
Statement statement = connection.createStatement();
int result = statement.executeUpdate(query);
System.out.println("Database is ready for use");
} catch (SQLException e1) {
e1.printStackTrace();
}
finally {
if (connection != null)
try {
connection.close();
System.out.println("Connection closed");
} catch (SQLException e) {
e.printStackTrace();
}
运行后我得到了
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 'use Contacts;CREATE TABLE IF NOT EXISTS info(first_name TEXT,surname TEXT,login ' at line 1
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:400)
at com.mysql.jdbc.Util.getInstance(Util.java:383)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:980)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2541)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1604)
at com.mysql.jdbc.StatementImpl.executeUpdate(StatementImpl.java:1535)
at task.DatabaseManager.createDatabase(DatabaseManager.java:28)
at task.Test.main(Test.java:14)
Connection closed
已创建数据库,但表未创建。所以问题出在那里。还有一件事。当我将这个查询粘贴到MySQL Workbench并执行它时 - 它完美地工作并创建我需要的一切。
答案 0 :(得分:2)
做一批:
String[] sqlStatements = new String[] {
"CREATE DATABASE IF NOT EXISTS Contacts",
"use Contacts",
"CREATE TABLE IF NOT EXISTS info(first_name TEXT,surname TEXT,login TEXT,email TEXT,phone_number INT NOT NULL primary key)" };
Connection connection = null;
try {
connection = createConnection(user, password);
Statement statement = connection.createStatement();
for (String sqlStatement : sqlStatements) {
statement.addBatch(sqlStatement);
}
int[] result = statement.executeBatch();
for (int i = 0; i < result.length; i++) {
System.out.println(sqlStatements[i] + " - " + result[i]);
}
System.out.println(result);
System.out.println("Database is ready for use");
} catch (SQLException e1) {
e1.printStackTrace();
} finally {
if (connection != null)
try {
connection.close();
System.out.println("Connection closed");
} catch (SQLException e) {
e.printStackTrace();
}
}
答案 1 :(得分:1)
您的问题可能是由于您没有在数据库连接上设置多个查询属性,因此它只执行第一个语句。尝试修改数据库URL,如下所示:
jdbc:mysql:<your_url>?allowMultiQueries=true