Example usage for org.apache.ibatis.jdbc ScriptRunner ScriptRunner

List of usage examples for org.apache.ibatis.jdbc ScriptRunner ScriptRunner

Introduction

In this page you can find the example usage for org.apache.ibatis.jdbc ScriptRunner ScriptRunner.

Prototype

public ScriptRunner(Connection connection) 

Source Link

Usage

From source file:br.jpe.dallahits.script.core.InicializadorDB.java

/**
 * Executa criao das tabelas do banco/*from ww w.java 2  s .c om*/
 *
 * @throws IOException Falha ao ler Script SQL de gerao das tabelas
 * @throws DAOException Falha ao executar a criao das tabelas
 */
public void exec() throws IOException, DAOException {
    Conexao conn = null;
    try {
        // Define as propriedades da conexo
        ConnManager.setProperties(ContextUtils.lePropriedadesConexao(dirBase));
        // Cria conexo e prepara o ScriptRunner
        conn = ConnFactory.criaConexaoTransacao();
        criaBaseDados();
        ScriptRunner sr = new ScriptRunner(conn.get());
        // Roda os scripts
        criaTabelas(sr);
        inicializaDados(sr);
        criaViews(sr);
    } catch (IOException e) {
        throw e;
    } finally {
        // Encerra conexo
        DBUtils.close(conn);
    }
}

From source file:com.athena.peacock.installer.Installer.java

License:Apache License

/**
 * <pre>/*from ww w.  j  a  v a  2  s  .com*/
 * DataBase  .
 * </pre>
 * @param driver
 * @param url
 * @param schema
 * @param masterUser
 * @param masterPass
 * @param peacockUser
 * @param peacockPass
 * @throws Exception
 */
@SuppressWarnings("resource")
public static void initData(String driver, String url, String schema, String masterUser, String masterPass,
        String peacockUser, String peacockPass) throws Exception {
    InputStream is = Installer.class.getResourceAsStream("/db_init.sql");

    Scanner s = new Scanner(is, "UTF-8").useDelimiter("\\A");
    String contents = s.hasNext() ? s.next() : "";

    contents = contents.replaceAll("\\$\\{db_schema\\}", schema);
    contents = contents.replaceAll("\\$\\{username\\}", peacockUser);
    contents = contents.replaceAll("\\$\\{password\\}", peacockPass);

    Class.forName(driver);
    new ScriptRunner(DriverManager.getConnection(url, masterUser, masterPass))
            .runScript(new StringReader(contents));
}

From source file:com.collective.messages.persistence.dao.BaseDataTestCase.java

License:Apache License

public static void runScript(DataSource ds, String resource) throws IOException, SQLException {
    Connection connection = ds.getConnection();
    try {//from w w  w .  ja v  a  2 s. c o  m
        ScriptRunner runner = new ScriptRunner(connection);
        runner.setAutoCommit(true);
        runner.setStopOnError(false);
        runner.setLogWriter(null);
        runner.setErrorLogWriter(null);
        runScript(runner, resource);
    } finally {
        connection.close();
    }
}

From source file:com.daemon.SearchTermTest.java

License:Open Source License

@BeforeClass
public static void prepareDatabase() throws SQLException {
    try {/*from  w  w  w . ja  v a 2s  .  c  o m*/
        transactor = new Transactor();
        connection = DriverManager.getConnection(transactor.getDbUrl());

        // Prepare database
        Reader reader = Resources.getResourceAsReader("DaemonDump.sql");

        ScriptRunner sr = new ScriptRunner(connection);

        sr.setDelimiter(";");
        sr.setLogWriter(null);
        sr.setErrorLogWriter(null);
        sr.runScript(reader);

        connection.commit();
        reader.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        connection.close();
    }
}

From source file:com.daemon.TransactorTest.java

License:Open Source License

@BeforeClass
public static void beforeClass() {
    try {/*from w w  w.  j  a va  2 s.  c  o  m*/
        _dbPath = Transactor.DATABASE_PROPERTY_PATH;
        URL url = Resources.getResourceURL("database.properties");
        Transactor.DATABASE_PROPERTY_PATH = URLDecoder.decode(url.getFile(), "UTF-8");
        _trans = new Transactor();
        _trans.connect();

        // Prepare database
        Reader reader = Resources.getResourceAsReader("DaemonDump.sql");

        ScriptRunner sr = new ScriptRunner(_trans.getConnection());

        sr.setDelimiter(";");
        sr.setLogWriter(null);
        sr.setErrorLogWriter(null);
        sr.runScript(reader);

        _trans.getConnection().commit();
        reader.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } catch (SQLException sqle) {
        sqle.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.github.cchacin.cucumber.steps.DatabaseSteps.java

License:Apache License

@Given("^I have the following sql script \"([^\"]*)\"$")
public void I_have_the_following_sql_script(final String script) throws Throwable {
    new ScriptRunner(this.destination.getConnection()).runScript(new BufferedReader(
            new FileReader(Thread.currentThread().getContextClassLoader().getResource(script).getPath())));
}

From source file:com.google.enterprise.connector.db.DBTestBase.java

License:Apache License

/**
 * Executes the database script./*from  w  w w  .  j a v a  2s .com*/
 *
 * @param scriptPath path of SQL script file
 */
protected void runDBScript(String scriptPath) throws Exception {
    ScriptRunner runner = new ScriptRunner(dbConnection);
    runner.setStopOnError(true);
    runner.runScript(Resources.getResourceAsReader(scriptPath));
}

From source file:com.jcin.cms.web.setup.SetupController.java

/**
 * 3.?/*from ww  w.  j  ava  2  s .  c o  m*/
 * 
 * @return
 */
@RequestMapping(value = "/finish")
public String finish(@Valid Hostsetting hostsetting, BindingResult result, Model uiModel,
        HttpServletRequest httpServletRequest, HttpServletResponse response) {
    if (result.hasErrors()) {
        populateEditForm(uiModel, hostsetting);
        return "database";
    }

    URL url = SetupController.class.getClassLoader().getResource("");
    String filePath = url.getPath();
    filePath += "database" + File.separator + "db.sql";
    File file = new File(filePath);

    String dbdriver = "com.mysql.jdbc.Driver";
    String dburl = "jdbc:mysql://" + hostsetting.getHost() + ":" + hostsetting.getPort() + "/"
            + hostsetting.getDbname();
    Connection con = null; // ?
    //      PrintWriter out = null;
    String object = "";
    try {
        //         out = response.getWriter();
        Class.forName(dbdriver).newInstance();
        con = DriverManager.getConnection(dburl, hostsetting.getUsername(), hostsetting.getPassword()); // 2??

        ScriptRunner runner = new ScriptRunner(con);
        InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "UTF-8");
        runner.runScript(isr);

        if (con != null)
            object = "success";
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InstantiationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (!object.equals("success"))
            object = "?url,???????";
        //         out.write(object);
        //         out.flush();
        //         out.close();
    }
    return "redirect:/setup/success";
}

From source file:com.jcin.SetUpServlet.java

/**
 * ?//from   ww w . j ava 2  s  .  c om
 * 
 * @param request
 * @param response
 * @throws ServletException
 * @throws IOException
 */
public void setup(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String filePath = this.getServletConfig().getServletContext().getRealPath("/");
    filePath += "database" + File.separator + "createDatabase.sql";
    File file = new File(filePath);
    System.out.println(filePath);

    String host = request.getParameter("host");
    if (host == null || "".equals(host)) {
        host = "localhost";
    }

    String port = request.getParameter("port");
    if (port == null || "".equals(port)) {
        port = "3306";
    }

    String databasename = request.getParameter("databasename");
    if (databasename == null || "".equals(databasename)) {
        databasename = "test";
    }

    String user = request.getParameter("user");
    if (user == null || "".equals(user)) {
        user = "root";
    }

    String password = request.getParameter("password");
    if (password == null || "".equals(password)) {
        password = "qwer123456";
    }

    String dbdriver = "com.mysql.jdbc.Driver";
    String dburl = "jdbc:mysql://" + host + ":" + port + "/" + databasename;
    Connection con = null; // ?

    PrintWriter out = response.getWriter();

    String result = "fail";
    try {
        Class.forName(dbdriver).newInstance();
        con = DriverManager.getConnection(dburl, user, password); // 2??
        ScriptRunner runner = new ScriptRunner(con);
        Reader reader = new FileReader(file);
        runner.runScript(reader);

        result = "?";
    } catch (ClassNotFoundException e) {
        if (out == null) {
            out = response.getWriter();
        }
        result = "Could not found Class";
        e.printStackTrace();
    } // 1?CLASS ?
    catch (SQLException e) {
        if (out == null) {
            out = response.getWriter();
        }
        result = "?url,???????";
        e.printStackTrace();
    } catch (InstantiationException e) {
        if (out == null) {
            out = response.getWriter();
        }
        result = "";
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        if (out == null) {
            out = response.getWriter();
        }
        result = "??";
        e.printStackTrace();
    } finally {
        try {
            con.close();
        } catch (SQLException e) {
            result = "?";
            e.printStackTrace();
        }
    }

    if (out != null) {
        out.write("success");
        out.flush();
        out.close();
    }
    //      response.sendRedirect(request.getContextPath()
    //            + "/license.jsp");
}

From source file:com.persinity.common.db.SimpleRelDb.java

License:Apache License

@Override
public void executeScript(final String scriptName) {
    notEmpty("scriptName");

    if (scriptRunner == null) {
        scriptRunner = new ScriptRunner(getConnection());
        configure(scriptRunner);// w ww  .j  a v  a  2  s  . com
    }

    final ScriptRunner _scriptRunner = scriptRunner;
    final InputStream resourceStream = ClassLoader.getSystemResourceAsStream(scriptName);
    assertArg(resourceStream != null, "Failed to find script: {}", scriptName);
    final Reader scriptReader = new InputStreamReader(resourceStream);

    log("Executing DB script ", Collections.singletonList(scriptName));
    resource.accessAndAutoClose(new Accessor<Reader, Void>(scriptReader, scriptName) {
        @Override
        public Void access(final Reader resource) throws Exception {
            _scriptRunner.runScript(new BufferedReader(scriptReader));
            return null;
        }
    });

}