简体   繁体   English

java JDBC MYSQL 语法错误

[英]java JDBC MYSQL Syntax error

Well, I have got some problems with this sql syntax.好吧,我在这个 sql 语法上遇到了一些问题。

public static void newTable(String tableName, String columnName, String columnType) {

    try {

        PreparedStatement pst = conn.prepareStatement
                ("CREATE TABLE `" + tableName + "`" + "(`" +columnName + "`  `" +  columnType + "` )");

        //PreparedStatement pst = conn.prepareStatement("CREATE TABLE teszt2 ( asd VARCHAR(255) );" ); //THIS IS WORKING

        //pst.setString(1, tableName);
        //pst.setString(2, columnName);
        //pst.setString(3, columnType);
        pst.execute();

    } catch (SQLException e) {
        e.printStackTrace();
    }

}

This is the error that I got: Every time I run this I got this error.这是我得到的错误:每次我运行这个我都会得到这个错误。 I assume that I have some problems with my query.我假设我的查询有一些问题。 Although, the one without strings in it is working fine.虽然,其中没有字符串的工作正常。

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 '`VARCHAR(255)` )' at line 1

According to the comments in your code, this works:根据您的代码中的注释,这是有效的:

CREATE TABLE teszt2 ( asd VARCHAR(255) );

But this doesn't:但这不会:

CREATE TABLE `teszt2` ( `asd` `VARCHAR(255)` );

So I guess remove the back-ticks and use the "working" version:所以我想删除反引号并使用“工作”版本:

PreparedStatement pst = conn.prepareStatement
            ("CREATE TABLE " + tableName + " (" +columnName + " " +  columnType + " )");

Or at least remove them from the type specifier:或者至少从类型说明符中删除它们:

PreparedStatement pst = conn.prepareStatement
            ("CREATE TABLE `" + tableName + "`" + "(`" +columnName + "`  " +  columnType + " )");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM