如何使用Java在mysql中复制架构 [英] how to copy a schema in mysql using java

查看:87
本文介绍了如何使用Java在mysql中复制架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我需要复制一个带有表的模式,并将存储过程从基本方案复制到新模式.

in my application i need to copy a schema with its tables and store procedures from a base schemn to a new schema.

我正在寻找一种方法来实现这一目标. 我研究了使用cmd对mysqldump进行精确化,但这不是一个好的解决方案,因为我有一个客户端应用程序,这需要在客户端上注入服务器. 另一个选择是我自己使用show query植入. 这里的问题是我不需要从头开始实现所有功能,必须要解决的问题是我将需要根据外键安排表的顺序(因为如果表中有外键,则表i指向需要先创建).

i am looking for a way to implement this. i looked into exacting the mysqldump using cmd however it is not a good solution because i have a client side application and this requires an instillation of the server on the client side. the other option is my own implantation using show query. the problem here is that i need t implement it all from scratch and the must problematic part is that i will need to arrange the order of the tables according to there foreign key (because if there is a foreign key in the table, the table i am pointing to needs to be created first).

我还考虑过创建存储过程来做到这一点,但是我的SQL中的存储过程无法访问磁盘.

i also thought of creating a store procedure to do this but store procedures in my SQL cant access the disk.

也许有人对如何以其他方式实现这一想法有所了解?

perhaps someone has an idea on how this can be implemented in another way?

推荐答案

您可以尝试使用 Apache ddlutils .有一种方法可以将ddls从数据库导出到xml文件,然后将其重新导入.

You can try using the Apache ddlutils. There is a way to export the ddls from a database to an xml file and re-import it back.

api使用情况页上的示例提供了有关如何将架构导出到xml文件,从xml文件读取并将其应用于新数据库的示例.我在下面复制了这些功能,以及有关如何使用它来完成您所要的内容的一小段代码.您可以以此为起点并对其进行进一步优化.

The api usage page has examples on how to export schema to an xml file, read from xml file and apply it to a new database. I have reproduced those functions below along with a small snippet on how to use it to accomplish what you are asking for. You can use this as starting point and optimize it further.

DataSource sourceDb;
DataSource targetDb;

writeDatabaseToXML(readDatabase(sourceDb), "database-dump.xml");
changeDatabase(targetDb,readDatabaseFromXML("database-dump.xml"));



public Database readDatabase(DataSource dataSource)
{
   Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
   return platform.readModelFromDatabase("model");
}

public void writeDatabaseToXML(Database db, String fileName)
{
    new DatabaseIO().write(db, fileName);
}

public Database readDatabaseFromXML(String fileName)
{
    return new DatabaseIO().read(fileName);
}

public void changeDatabase(DataSource dataSource,
                           Database   targetModel)
{
    Platform platform = PlatformFactory.createNewPlatformInstance(dataSource);
    platform.createTables(targetModel, true, false);
}

这篇关于如何使用Java在mysql中复制架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆