dimanche 3 mai 2015

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

}

Aucun commentaire:

Enregistrer un commentaire