如何以编程方式删除H2数据库文件?

时间:2016-09-04 04:32:50

标签: java jdbc h2

我正在使用JDBC和H2数据库开发应用程序,有时需要删除数据库文件。有没有办法做到这一点?

2 个答案:

答案 0 :(得分:7)

是的,你可以!

请参阅此答案,找到H2存储数据库的文件夹(通常是用户的主目录):  Where does H2's Embedded Databases Store the data?

要删除它,可以使用org.h2.tools.DeleteDbFiles类,如下所示:

DeleteDbFiles.execute(dbDir, dbName, true);

有关DeleteDbFiles类的更多信息:http://www.h2database.com/javadoc/org/h2/tools/DeleteDbFiles.html

答案 1 :(得分:1)

private static async Task<string> ProcessResponseAsync(HttpResponseMessage response)
{
    var responseText = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

    if (response.StatusCode == System.Net.HttpStatusCode.OK)
    {
        return responseText;
    }

    return "Error: " + response.StatusCode + " Content: " + responseText;
}

 private static string GetUrl(string area, string method, string parameters)
 {
   if (!string.IsNullOrEmpty(parameters))
   {
     if (parameters[0] != '?')
     {
       parameters = $"?{parameters}";
     }
   }

   var address = ConfigurationManager.AppSettings["GatewayUrl"];
   var result = $"{address}/{area}/{method}{parameters}";            
   return result;
}

protected async Task<string> ExecuteRequestAsync(string area, string method, string parameters)
{                        
   var url = GetUrl(area, method, parameters);            
   var response = await _httpClient.GetAsync(url).ConfigureAwait(false);
   var result = await ProcessResponseAsync(response).ConfigureAwait(false);

   return result;
}