Mr Asker
Mr Asker

Reputation: 2380

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

I am trying to make my code to work smoothly but after the first insert and when the table exist I am always getting the this error com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'bus' already exists also after the creation of the fisr table and inserting the data this error occures?! I want just to check whether the table exists if not create the tabe and insert the data if yes just update the data.

Database class:

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();
        }
    }

}

Upvotes: 2

Views: 3886

Answers (2)

Jimmy
Jimmy

Reputation: 1051

You can fix the exception by using proper value (none, create, update...) for the property of hibernate.hbm2ddl.auto

<property name="hibernate.hbm2ddl.auto"> xxx </property> <!-- xxx : create, update, none -->

1. If the database has no tables and you want to create them at run-time (with Hibernate) you have two choice to set the value for the property hibernate.hbm2ddl.auto as either create or update

  • create - it will create all tables (if exist then first delete all the tables and then create all the tables again)
  • update - it will create only those table which are not exist in the database

2. If the database has all the tables and you don't want to create then then you have a choice to set the value for the property hibernate.hbm2ddl.auto as none

Upvotes: 0

lopushen
lopushen

Reputation: 1137

You may want to use CREATE TABLE IF NOT EXISTS syntax

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

This will hep you avoid such mistakes

But I would check if

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

work correctly, you may get the wrong result and perform wrong operation

Upvotes: 4

Related Questions