Итак, у меня есть файл schema.sql
, который содержит схему базы данных, и я хочу создать соответствующие таблицы при развертывании приложения.
ServletContextListener
выглядит так
/**
* 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();
}
}
}
и схема, подобная этой:
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;
Ошибка:
У вас ошибка в синтаксисе SQL; проверьте руководство, соответствующее вашей версии сервера MySQL, на предмет правильного синтаксиса, который можно использовать рядом с 'SET AUTOCOMMIT = 0;
НАЧАЛО СДЕЛКИ;
Так что мне кажется, что он не принимает несколько команд в кадре. Как правильно заставить это работать? Использование одного отдельного schema.sql
обязательно.