java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Table 'bus' already exists

标签 java sql

我试图让我的代码顺利工作,但是在第一次插入之后并且当表存在时,我总是收到此错误com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:表'bus'已经存在 也在创建 fisr 表并插入数据后发生此错误?!我只想检查表是否存在,如果不创建表并插入数据,如果是则更新数据。

数据库类:

package org.busTracker.serverSide;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Database {

    public void connection() {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("jar works :) ");

        } catch (ClassNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void insertData(String mac, int route, float latitude,
            float longitude) {

        connection();
        String host = "jdbc:mysql://localhost/busTracker";
        String user = "root";
        String password = "";

        try {
            Connection con = DriverManager.getConnection(host, user, password);

            // Create a statement
            Statement stt = con.createStatement();

            // Check whether table exists.
            boolean rest = stt.execute("SHOW TABLES like 'bus' ");

            if (rest) {

                PreparedStatement prep = con
                        .prepareStatement("REPLACE INTO bus(mac, route, latitude, longitude)"
                                + "VALUES( ?, ?, ? , ? )");
                prep.setString(1, mac);
                prep.setInt(2, route);
                prep.setFloat(3, latitude);
                prep.setFloat(4, longitude);
                prep.executeQuery();

            } else {

                // Create bus table
                stt.execute("CREATE TABLE bus"
                        + "(id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,"
                        + "mac VARCHAR(30) NOT NULL UNIQUE,"
                        + "route int(11) NOT NULL,"
                        + "latitude FLOAT(10,6) NOT NULL,"
                        + "longitude FLOAT(10,6) NOT NULL,"
                        + "created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)");

                PreparedStatement prep = con
                        .prepareStatement("REPLACE INTO bus(mac, route, latitude, longitude)"
                                + "VALUES( ?, ?, ? , ? )");
                prep.setString(1, mac);
                prep.setInt(2, route);
                prep.setFloat(3, latitude);
                prep.setFloat(4, longitude);
                prep.executeQuery();

            }

        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

最佳答案

您可能想要使用 CREATE TABLE IF NOT EXISTS 语法

https://dev.mysql.com/doc/refman/5.5/en/create-table.html

这将帮助您避免此类错误

但我会检查是否

 boolean rest = stt.execute("SHOW TABLES like 'bus' ");

工作正常,你可能会得到错误的结果并执行错误的操作

关于java - com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException : Table 'bus' already exists,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30020099/

相关文章:

java - 搜索大目录中的多个文件夹 - JAVA

java - 比较两个时间戳

sql - 在 SELECT 子句中的列之间使用相等性检查

mysql - on 子句中的未知列,无法在工作台中重现

java - Cassandra 不使用 native 方法

java - 失败 : IllegalArgumentException java.net.URISyntaxException:绝对 URI 中的相对路径:

java - 我如何限制 2D 数组用户输入并输出其余值 "."?

sql - 删除带有警告的重复项

sql - Postgresql - 将字符串与空值进行比较

.net - 包含括号的动态 LINQ 查询的问题