我尝试使用 JDBC 在 MySQL 数据库中创建一个 table
I trying to create a table in MySQL database using JDBC
我正在尝试在我的 MySQL 数据库中创建一个 table。这是我的代码:
//Table
public static void createTable() throws Exception
{
try {
Connection conn = getConnection();
PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXIST registration(id int NOT NULL AUTO_INCREMENT,first_name varchar(45),last_name(45),email varchar(320),password varchar(50),PRIMARY KEY(id))");
create.executeUpdate();
}
catch(Exception e) {
System.out.println(e);
}
finally {
System.out.println("System Updated");
};
}
//End of Table
这是我遇到的错误
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 'EXIST registration(id int NOT NULL AUTO_INCREMENT,first_name varchar(45),last_name(45),email var' at line 1
first_name varchar(45),last_name(45)
last_name 没有与之关联的数据类型
尝试
last_name varchar(45)
这个查询有两处错误:
- 应该是
if not exits
,最后加一个"s"。
last_name
列缺少类型(大概是 varchar
?)
将它们放在一起,您的 SQL 应该如下所示:
CREATE TABLE IF NOT EXISTS -- First issue
registration
(id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(45),
last_name VARCHAR(45), -- Second issue
email VARCHAR(320),
password VARCHAR(50),
PRIMARY KEY(id)
)
您的 last_name 列缺少数据类型并且 EXISTS 拼写错误。您已经离开 's'.
我正在尝试在我的 MySQL 数据库中创建一个 table。这是我的代码:
//Table
public static void createTable() throws Exception
{
try {
Connection conn = getConnection();
PreparedStatement create = conn.prepareStatement("CREATE TABLE IF NOT EXIST registration(id int NOT NULL AUTO_INCREMENT,first_name varchar(45),last_name(45),email varchar(320),password varchar(50),PRIMARY KEY(id))");
create.executeUpdate();
}
catch(Exception e) {
System.out.println(e);
}
finally {
System.out.println("System Updated");
};
}
//End of Table
这是我遇到的错误
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 'EXIST registration(id int NOT NULL AUTO_INCREMENT,first_name varchar(45),last_name(45),email var' at line 1
first_name varchar(45),last_name(45)
last_name 没有与之关联的数据类型
尝试
last_name varchar(45)
这个查询有两处错误:
- 应该是
if not exits
,最后加一个"s"。 last_name
列缺少类型(大概是varchar
?)
将它们放在一起,您的 SQL 应该如下所示:
CREATE TABLE IF NOT EXISTS -- First issue
registration
(id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(45),
last_name VARCHAR(45), -- Second issue
email VARCHAR(320),
password VARCHAR(50),
PRIMARY KEY(id)
)
您的 last_name 列缺少数据类型并且 EXISTS 拼写错误。您已经离开 's'.