Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6993881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:49:14+00:00 2026-05-27T19:49:14+00:00

So I’ve got a file schema.sql which contains the schema of the database and

  • 0

So I’ve got a file schema.sql which contains the schema of the database and I want to create the tables accordingly when the application is deployed.

The ServletContextListener looks like this

/**
 * This class doesn't really belong to the Ui package, but it does depend on
 * servlet technology like all the other classes here, so I've just put it here for the moment.
 * 
 * The class may be moved in the future
 * 
 * When the application is deployed and started, this class connects to the database,
 * checks if the environment is compatible, checks various other things like whether
 * Core.User.HASH_ALGO or Core.User.STRING_ENCODING are supported (see Core.User for
 * details).
 */
package Ui;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContext;

import com.mysql.jdbc.Driver;
import java.sql.*;

import java.io.*;
import java.net.URL;

/**
 * Web application lifecycle listener.
 */
public class AppContextListener implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext s = sce.getServletContext();
        String db_uri = s.getInitParameter("DB_HOST");
        String db_user = s.getInitParameter("DB_USERNAME");
        String db_pass = s.getInitParameter("DB_PASSWORD");
        try {
          Connection connection = DriverManager.getConnection(db_uri, db_user, db_pass);
          //TODO check if version is compatible ;-), set app as unusable if necessary
          s.setAttribute("db", connection);
          Statement st = null;
          if(null != connection) {
              st = connection.createStatement();
              String schema = null;
              try {
                InputStream str = s.getResourceAsStream("/WEB-INF/sql/schema.sql");
                schema = readStream(str);

              }
              catch(Exception e) {
                  //TODO set global application state as unusable, with a message
              }
              finally {
                int i = st.executeUpdate(schema);
             }
          }
        } catch(SQLException e) {
            //TODO set global application state as unusable, with a message
        }
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        //TODO disconnect from the DB
    }

    protected String readStream(InputStream stream) {
        BufferedInputStream bis = new BufferedInputStream(stream);
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        try {
            int result = bis.read();
            while(result != -1) {
              byte b = (byte)result;
              buf.write(b);
              result = bis.read();
            }
        }
        catch(IOException e) {
            return null;
        }
        finally {
            return buf.toString();
        }

    }
}

and the schema like this:

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT=0;
START TRANSACTION;
SET time_zone = "+00:00";

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL,
  `username` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `password` varchar(40) COLLATE utf8_bin NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
COMMIT;

The error is:

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 ‘SET AUTOCOMMIT=0;
START TRANSACTION;

So it looks to me as if it doesn’t accept multiple commands in a shot. What is the right way to get it working? Using one single separate schema.sql is imperative.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:49:15+00:00Added an answer on May 27, 2026 at 7:49 pm

    I don’t have enough environment to test this, but have you tried to do this as a batch using addBatch(schema) and executeBatch()?

    Otherwise, you may be stuck splitting the command up using schema.split(";") and execute one at a time. If you do this, be sure to not leave the tran open if you fall out of the loop.

    Best of luck.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use html5's new tag to play a wav file (currently only supported
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i want to parse a xhtml file and display in UITableView. what is the
I have a reasonable size flat file database of text documents mostly saved in
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.