简体   繁体   English

JAVA mysql INSERT错误

[英]JAVA mysql INSERT error

 private final static String INSERT = "INSERT INTO electric_usage" +
        "(objId, useTime, name, usage) " +
        "VALUES (?, ?, ?, ?)";    

    public static boolean insertUsage(int index, Timestamp time, String name, double usage) {
        Connection con = null;

        try {
            con = DBManager.getInstance().getConnection();

            PreparedStatement stmt = con.prepareStatement(INSERT);

            java.util.Date today = new java.util.Date();


            stmt.setInt(1, index);
            stmt.setTimestamp(2, time);
            stmt.setString(3, name);
            stmt.setDouble(4, usage);
            stmt.addBatch();

            stmt.executeBatch();
            stmt.close();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            DBManager.getInstance().close();
        }

        return true;


    }

make INSERT query like this but this code occur syntax error 像这样生成INSERT查询,但此代码出现语法错误

other load query is work fine only this INSERT quert occur error 其他加载查询工作正常只有这个INSERT队列发生错误

im trying to INSERT query in console it occur same error 我试图在控制台中INSERT查询它发生相同的错误

my query is wrong? 我的查询错了?

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 'usage) VALUES (192, '2015-09-10 13:55:57', 'test', 0.0045196452704869055)' at line 1

table is 表是

objId(int length 8 not null)
useTime(timestamp length 0 not null)
name (varchar length 255 not null)
usage (double length 11 not null)

That's because your column names index/usage are all MySQL Reserve words and so needs to be escaped using backtique like below 这是因为您的列名index/usage都是MySQL Reserve单词 ,因此需要使用backtique进行转义,如下所示

INSERT INTO electric_usage (`index`, `time`, `name`, `usage`) 

Always avoid using table/column name as reserve word else you will have to suffer likewise. 始终避免使用表/列名作为保留字,否则您将不得不忍受。 Use proper naming convention like prefix t_ for table names and c_ for column names. 使用适当的命名约定,如前缀t_表示表名, c_表示列名。

index is a reserved word so you should not use it to name a column. index是保留字,因此不应使用它来命名列。 List of reserved words here: http://dev.mysql.com/doc/refman/5.6/en/keywords.html 这里保留字列表: http//dev.mysql.com/doc/refman/5.6/en/keywords.html

index is reserved word in mysql you can't use mysql reserved words.when you write query in query browser than reserved words shows in blue. 索引是mysql中的保留字,你不能使用mysql保留字。当你在查询浏览器中写查询时,保留字显示为蓝色。 so please take care about this.if you write query in java coding directly you can't find these type of issues. 所以请注意这一点。如果您直接在java编码中编写查询,则无法找到这些类型的问题。

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

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