1

Can somebody explain what I am doing wrong. I want to make table from JDBC, I executed the query in MySQLWorkBench works fine. When I want to execute it from JDBC constant gives SQLSyntaxErrorException for every kind of query. However when I execute this code above works fine.

preparedStatement = conn.prepareStatement("show tables");
ResultSet resultSet = preparedStatement.executeQuery();

My code

    private static final String DB_USERNAME = "root";
    private static final String DB_PASSWORD = "root";
    private static final String DB_NAME = "lab4t";

    private Connection connection;

    public JdbcMenuDao() {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver"); // driver class betoltese
            connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/"+DB_NAME+"?createDatabaseIfNotExist=true",DB_USERNAME,DB_PASSWORD);

            Statement statement = connection.createStatement();
            LOG.info("Connection Succesful");

            String initializeQ=("SET FOREIGN_KEY_CHECKS=0;DROP TABLE IF EXISTS Restaurants; SET FOREIGN_KEY_CHECKS=1;"
                    + "CREATE TABLE Restaurants("
                    + "id bigint PRIMARY KEY not null auto_increment,"
                    + "restaurantName varchar(64)"
                    + ") engine=InnoDB;"
                    + "desc Restaurants;");

             int resultSet=statement.executeUpdate(initializeQ);
            } catch (ClassNotFoundException | SQLException ex) {
            LOG.error("Could not connect",ex);
            throw new DaoException("Could not connect",ex);
        }
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
MrNobody
  • 535
  • 8
  • 24
  • 1
    You are executing multiple statements at once. Does this question help: https://stackoverflow.com/questions/10797794/multiple-queries-executed-in-java-in-single-statement ? – Luke Woodward Nov 22 '20 at 09:19

0 Answers0