JDBC:创建表查询语法错误

时间:2016-10-17 05:12:28

标签: java mysql jdbc

我正在尝试使用JDBC执行创建查询。我有一个方法创建查询,然后我执行它,但它显示我语法错误。下面是堆栈跟踪:

com.mysql.jdbc.exceptions.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 'supplierId varchar,supplierUrl varchar,totalActivities varchar,activityName varc' at line 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3243)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1343)
at com.mysql.jdbc.Statement.executeUpdate(Statement.java:1260)

现在生成的查询是:

create table demo ( ID INTEGER PRIMARY KEY AUTO_INCREMENT,supplierName varchar,supplierId varchar,supplierUrl varchar,totalActivities varchar,activityName varchar,activityPrice varchar,tourCode varchar,starRating varchar,totalReviews varchar,geography varchar,duration varchar,category varchar,subCategory varchar);

以下是生成此查询的方法:

private static String getCreateTableQuery(String tableName, String columnData) {
    StringBuilder sqlStatement = new StringBuilder("");

    sqlStatement.append("create table " + tableName + " ( ID INTEGER PRIMARY KEY AUTO_INCREMENT,");

    String[] columns = columnData.split(">");  // columns are separated by >
    for (int i = 0; i < columns.length; i++) {
        sqlStatement.append(columns[i] + " varchar");
        if (i != columns.length - 1) { // no commas after last column
            sqlStatement.append(",");
        }
    }
    sqlStatement.append(");");
    return sqlStatement.toString();
}

这就是执行查询的方式:

SessionImpl sessionImpl = (SessionImpl) getSessionFactory().openSession(); 
    Connection conn = (Connection) sessionImpl.connection();

    Statement statement = (Statement) conn.createStatement();
    statement.executeUpdate(query);
    sessionImpl.close();
    conn.close();

无法理解语法错误。有人可以解释一下吗?

2 个答案:

答案 0 :(得分:0)

在Mysql中,您需要为varchar定义一个长度。看看这里:

Why does VARCHAR need length specification?

我没有看到您的Java代码有问题。修复你的create table语句,你可能会没事的。

答案 1 :(得分:0)

我认为您必须传递varchar 字段的最大长度:

请检查一下您的查询是这样的:

create table demo ( ID INTEGER PRIMARY KEY AUTO_INCREMENT,supplierName varchar(255),supplierId varchar(255),supplierUrl varchar(255),totalActivities varchar(255),activityName varchar(255),activityPrice varchar(255),tourCode varchar(255),starRating varchar(255),totalReviews varchar(255),geography varchar(255),duration varchar(255),category varchar(255),subCategory varchar(255));

这是插入查询:

insert into demo
    ( supplierName, supplierId, supplierUrl, totalActivities, activityName, 
    activityPrice, tourCode, starRating, totalReviews, geography, duration, 
    category, subCategory)
    values
    (supplierName, supplierId, supplierUrl, totalActivities, activityName, 
    activityPrice, tourCode, starRating, totalReviews, geography, duration, 
    category, subCategory)