@Override public void createUser(String user, String password, String adminUser, String adminPassword) throws MojoExecutionException { DriverManagerUtil.registerDriver(DRIVER); Connection conn = null; Statement stmt = null; try { conn = DriverManager.getConnection(url, adminUser, adminPassword); stmt = conn.createStatement(); if (existsUser(conn, user)) { return; } try { stmt.execute("DROP USER '" + user + "'"); } catch (SQLException ignore) { // DROP USERに失敗しても気にしない } stmt.execute("CREATE USER '" + user + "' IDENTIFIED BY '" + password + "'"); stmt.execute("GRANT ALL ON *.* TO '" + user + "'"); } catch (SQLException e) { throw new MojoExecutionException("CREATE USER実行中にエラー", e); } finally { StatementUtil.close(stmt); ConnectionUtil.close(conn); } }
/** * 準備されたステートメントを返します。 * * @param sql SQL * @return 準備されたステートメント */ public PreparedStatement getPreparedStatement(String sql) { if (connection != null && preparedStatement != null) { StatementUtil.close(preparedStatement); } preparedStatement = ConnectionUtil.prepareStatement(connection, sql); return preparedStatement; }
private boolean existsUser(Connection conn, String user) throws SQLException { PreparedStatement stmt = null; try { stmt = conn.prepareStatement("SELECT count(*) AS num FROM mysql.user WHERE User=?"); stmt.setString(1, user); ResultSet rs = stmt.executeQuery(); rs.next(); return (rs.getInt("num") > 0); } finally { StatementUtil.close(stmt); } }
public boolean isAutoIncrement( Connection connection, String catalogName, String schemaName, String tableName, String columnName) throws SQLException { String fullTableName = TableUtil.buildFullTableName(catalogName, schemaName, tableName); String sql = "select " + columnName + " from " + fullTableName + " where 1 = 0"; logger.debug(sql); PreparedStatement ps = ConnectionUtil.prepareStatement(connection, sql); try { ResultSet rs = ps.executeQuery(); try { ResultSetMetaData rsMetaData = rs.getMetaData(); return rsMetaData.isAutoIncrement(1); } finally { ResultSetUtil.close(rs); } } finally { StatementUtil.close(ps); } }