- 浏览: 212611 次
- 性别:
- 来自: 杭州
文章分类
最新评论
-
Wangwei86609:
非常好的规则引擎框架,支持决策树和多线程运行规则https:/ ...
规则引擎 -
hzxlb910:
真详细,收藏哈
maven setting.xml配置说明 -
东方胜:
[b][/b]
脚本语言 Tcl -
345161974:
hyw520110 写道345161974 写道这个Visua ...
Visual Tcl Binary 完整版(完美中文支持) -
hyw520110:
345161974 写道这个Visual Tcl Binary ...
Visual Tcl Binary 完整版(完美中文支持)
解决的问题:要把原始数据库中的数据导出到其它的数据库表中去。
原始数据库可能是MYSQL, SQLSERVER或者其它的数据库,这些数据库的Schema是根据实体定义文件以及类型定义文件的。
数据要导入的数据库是HSQLDB,现有的ORM工具要求先建立对象到数据库结构的隐射,以及需要建立配置环境才能实现将对象隐射到数据库中的表结构中去。
因此,要解决数据导出导入问题,首先,要根据实体定义文件生成HSQL DB的Schema,执行建表操作,然后再把从原数据库中选择出来的实体插入到HSQL DB数据库中去。
因此,要解决的问题主要有两个:
1. 自动建立HSQL DB Schema
2. 自动将实体存储到HSQL DB
约束条件:
1. HSQL DB Schema和原数据库的Schema应该是类似的,能够兼容从原数据库中导入来的数据。数据域的名称是一样的。
2. 实体存储的时候可以借鉴ORM存储的方式。
解决技术:
1. Schema自动生成。新建HSQLDB.xml数据类型的映射文件,新建HSQL DB的Schema(create table)的语法,根据实体定义文件生成HSQL DB对应的Schema。
2. 实体存储。参考实体工具存储实体的方式。将实体看成JAVA BEAN,由于实体定义文件中定义了实体的域值。可以根据数据类型定义文件找到实体的域值所对应的JAVA TYPE。利用TYPE的nullSafeSet使得可以安全的设置PreparedStatement所对应的INSERT语句的参数。我最头疼的
通过
调用Concrete Type中的nullSafeSet就解决了这个问题,真的是太爽了。
两个问题都解决了。最后看一下我的设计,自认为还比较优美:)
AbstractDataExporter是抽象的数据导出类,有两个Template模板方法,定义了数据导出的步骤。具体的数据导出类,只要实现抽象方法就可以了,具体类相当简单。
AbstractDataExporter借助SchemaGenerator来完成Schema生成操作:),顺便也完成了Drop Table语句的生成,以及INSERT 语句的生成。这些方法子类都不需要重写。
下面是子类的示例:代码很简单吧:)
SchemaGenerator:通过解析实体定义xml,以及数据类型定义xml来得到相关的Schema以及INSERT, DROP TABLE语句:)
原始数据库可能是MYSQL, SQLSERVER或者其它的数据库,这些数据库的Schema是根据实体定义文件以及类型定义文件的。
数据要导入的数据库是HSQLDB,现有的ORM工具要求先建立对象到数据库结构的隐射,以及需要建立配置环境才能实现将对象隐射到数据库中的表结构中去。
因此,要解决数据导出导入问题,首先,要根据实体定义文件生成HSQL DB的Schema,执行建表操作,然后再把从原数据库中选择出来的实体插入到HSQL DB数据库中去。
因此,要解决的问题主要有两个:
1. 自动建立HSQL DB Schema
2. 自动将实体存储到HSQL DB
约束条件:
1. HSQL DB Schema和原数据库的Schema应该是类似的,能够兼容从原数据库中导入来的数据。数据域的名称是一样的。
2. 实体存储的时候可以借鉴ORM存储的方式。
解决技术:
1. Schema自动生成。新建HSQLDB.xml数据类型的映射文件,新建HSQL DB的Schema(create table)的语法,根据实体定义文件生成HSQL DB对应的Schema。
2. 实体存储。参考实体工具存储实体的方式。将实体看成JAVA BEAN,由于实体定义文件中定义了实体的域值。可以根据数据类型定义文件找到实体的域值所对应的JAVA TYPE。利用TYPE的nullSafeSet使得可以安全的设置PreparedStatement所对应的INSERT语句的参数。我最头疼的
- public void setString(PreparedStatement ps, int index, String value) throws SQLException{
- if(null!=value)
- ps.setString(index, value);
- else
- ps.setNull(index, Types.VARCHAR);
- }
- public void setInteger(PreparedStatement ps, int index, Integer value) throws SQLException{
- if(null!=value)
- ps.setInt(index, value);
- else
- ps.setNull(index, Types.INTEGER);
- }
public void setString(PreparedStatement ps, int index, String value) throws SQLException{ if(null!=value) ps.setString(index, value); else ps.setNull(index, Types.VARCHAR); } public void setInteger(PreparedStatement ps, int index, Integer value) throws SQLException{ if(null!=value) ps.setInt(index, value); else ps.setNull(index, Types.INTEGER); }
通过
property.nullSafeSet(ps, PropertyUtils.getProperty(element, fields[i].getName()), i+1);
调用Concrete Type中的nullSafeSet就解决了这个问题,真的是太爽了。
两个问题都解决了。最后看一下我的设计,自认为还比较优美:)
AbstractDataExporter是抽象的数据导出类,有两个Template模板方法,定义了数据导出的步骤。具体的数据导出类,只要实现抽象方法就可以了,具体类相当简单。
AbstractDataExporter借助SchemaGenerator来完成Schema生成操作:),顺便也完成了Drop Table语句的生成,以及INSERT 语句的生成。这些方法子类都不需要重写。
- package ...
- import ...
- /**
- * 抽象的数据导出器。
- * 将原始数据库中的数据,导出到HSQLDB中的一个原始数据库中去。
- * @author wanzhigang
- *
- */
- public abstract class AbstractDataExporter {
- protected String entityName; //数据库表所对应的实体名称
- protected Connection connection;
- public AbstractDataExporter(Connection connection, String entityName){
- this.connection = connection;
- this.entityName = entityName;
- }
- /**
- * 对应的HSQL DB表的Schema。
- * @return
- */
- public String getSchema(){
- return SchemaGenerator.instance.generateHSQLSchema(entityName);
- }
- /**
- * 对应的HSQL DB表的Drop Table语句。
- * @return
- */
- public String getDropTableSQL(){
- return SchemaGenerator.instance.generateDropTableSQL(entityName);
- }
- /**
- * 对应的HSQL DB表的Insert语句。
- * @return
- */
- public String getInsert() {
- return SchemaGenerator.instance.generateInsertSQL(entityName);
- }
- /**
- * 将数据导出到HSQLDB中去。
- * @throws DataExporterException
- */
- protected void exportData2HSQLDBTemplate(Integer[] projectIds) throws DataExporterException{
- if(!createHSQLTable()) throw new DataExporterException("CreateHSQLTable Error");
- List orginalDataList = importDataTemplate(projectIds);
- if(null==orginalDataList) throw new DataExporterException("ReadOrginalData Error");
- if(!insert2HSQLDB(orginalDataList)) throw new DataExporterException("InsertInto HSQLDB Error");
- }
- /**
- * 将原始数据的实体插入到HSQLDB中去。
- * @param orginalDataList
- * @param insert
- * @return
- */
- protected boolean insert2HSQLDB(List orginalDataList){
- try {
- connection.setAutoCommit(false);
- for (Iterator iter = orginalDataList.iterator(); iter.hasNext();) {
- BaseObject element = (BaseObject) iter.next();
- PreparedStatement ps = connection.prepareStatement(getInsert());
- if(!setInsertParameter(ps, element)) return false;
- if(-1 == ps.executeUpdate()){
- Debug.logError("Insert into HSQLDB error");
- return false;
- }
- ps.close();
- }
- connection.commit();
- return true;
- } catch (SQLException e) {
- Debug.logError("SQL Exception in connect to HSQL DB");
- try {
- connection.rollback();
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- return false;
- }
- }
- /**
- * 设置插入到HSQL DB数据库中INSERT语句对应的?的数值。
- * @param ps
- * @param element 要插入的数据实体
- */
- private boolean setInsertParameter(PreparedStatement ps, BaseObject element) {
- Entity entity = (Entity) SchemaGenerator.instance.getEntityMap().get(entityName);
- Map fieldTypeDefMap = SchemaGenerator.instance.getFieldTypeDefMap();
- Field[] fields = entity.getField();
- for (int i = 0; i < fields.length; i++) {
- FieldTypeDef fieldTypeDef = (FieldTypeDef)fieldTypeDefMap.get(fields[i].getType());
- try {
- PropertyMetaData property = new PropertyMetaData(fields[i].getName(),
- TypeFactory.getType(fieldTypeDef.getJavaType())
- );
- property.nullSafeSet(ps, PropertyUtils.getProperty(element, fields[i].getName()), i+1);
- } catch (TypeNotSupportException e) {
- Debug.logError("Type Not Support.");
- return false;
- }catch (JDBCException e) {
- Debug.logError("JDBC Exception.");
- return false;
- }catch (IllegalAccessException e) {
- Debug.logError("IllegalAccessException. ");
- return false;
- } catch (InvocationTargetException e) {
- Debug.logError("InvocationTargetException.");
- return false;
- } catch (NoSuchMethodException e) {
- Debug.logError("NoSuchMethodException.");
- return false;
- }
- }
- return true;
- }
- /**
- * 从原始数据库中读取原始数据。是根据HQL来构造导出语句,还是根据Entity工具的Criteria来构造导出语句,应该由用户来选择。
- * @param projectIds 项目ID
- * @param select Select语句
- * @return 原始数据的实体列表
- */
- protected List importDataTemplate(Integer[] projectIds){
- String hql = createSelectHQL(projectIds);
- HqlReturnBuilder hqlRb = new HqlReturnBuilder(hql);
- try {
- return RecordContainer.doSelect(new Criteria(), hqlRb);
- } catch (QueryException e) {
- e.printStackTrace();
- return null;
- }
- }
- /**
- * 构造从原始数据库中取数据的SELECT语句。
- * @param projectIds
- * @return
- */
- protected abstract String createSelectHQL(Integer[] projectIds);
- /**
- * 创建HSQL表。
- * @return
- */
- protected boolean createHSQLTable(){
- PreparedStatement ps;
- try {
- ps = connection.prepareStatement(getDropTableSQL());
- ps.executeUpdate();
- ps.close();
- ps = connection.prepareStatement(getSchema());
- ps.executeUpdate();
- ps.close();
- return true;
- } catch (SQLException e) {
- e.printStackTrace();
- return false;
- }
- }
- }
package ... import ... /** * 抽象的数据导出器。 * 将原始数据库中的数据,导出到HSQLDB中的一个原始数据库中去。 * @author wanzhigang * */ public abstract class AbstractDataExporter { protected String entityName; //数据库表所对应的实体名称 protected Connection connection; public AbstractDataExporter(Connection connection, String entityName){ this.connection = connection; this.entityName = entityName; } /** * 对应的HSQL DB表的Schema。 * @return */ public String getSchema(){ return SchemaGenerator.instance.generateHSQLSchema(entityName); } /** * 对应的HSQL DB表的Drop Table语句。 * @return */ public String getDropTableSQL(){ return SchemaGenerator.instance.generateDropTableSQL(entityName); } /** * 对应的HSQL DB表的Insert语句。 * @return */ public String getInsert() { return SchemaGenerator.instance.generateInsertSQL(entityName); } /** * 将数据导出到HSQLDB中去。 * @throws DataExporterException */ protected void exportData2HSQLDBTemplate(Integer[] projectIds) throws DataExporterException{ if(!createHSQLTable()) throw new DataExporterException("CreateHSQLTable Error"); List orginalDataList = importDataTemplate(projectIds); if(null==orginalDataList) throw new DataExporterException("ReadOrginalData Error"); if(!insert2HSQLDB(orginalDataList)) throw new DataExporterException("InsertInto HSQLDB Error"); } /** * 将原始数据的实体插入到HSQLDB中去。 * @param orginalDataList * @param insert * @return */ protected boolean insert2HSQLDB(List orginalDataList){ try { connection.setAutoCommit(false); for (Iterator iter = orginalDataList.iterator(); iter.hasNext();) { BaseObject element = (BaseObject) iter.next(); PreparedStatement ps = connection.prepareStatement(getInsert()); if(!setInsertParameter(ps, element)) return false; if(-1 == ps.executeUpdate()){ Debug.logError("Insert into HSQLDB error"); return false; } ps.close(); } connection.commit(); return true; } catch (SQLException e) { Debug.logError("SQL Exception in connect to HSQL DB"); try { connection.rollback(); } catch (SQLException e1) { e1.printStackTrace(); } return false; } } /** * 设置插入到HSQL DB数据库中INSERT语句对应的?的数值。 * @param ps * @param element 要插入的数据实体 */ private boolean setInsertParameter(PreparedStatement ps, BaseObject element) { Entity entity = (Entity) SchemaGenerator.instance.getEntityMap().get(entityName); Map fieldTypeDefMap = SchemaGenerator.instance.getFieldTypeDefMap(); Field[] fields = entity.getField(); for (int i = 0; i < fields.length; i++) { FieldTypeDef fieldTypeDef = (FieldTypeDef)fieldTypeDefMap.get(fields[i].getType()); try { PropertyMetaData property = new PropertyMetaData(fields[i].getName(), TypeFactory.getType(fieldTypeDef.getJavaType()) ); property.nullSafeSet(ps, PropertyUtils.getProperty(element, fields[i].getName()), i+1); } catch (TypeNotSupportException e) { Debug.logError("Type Not Support."); return false; }catch (JDBCException e) { Debug.logError("JDBC Exception."); return false; }catch (IllegalAccessException e) { Debug.logError("IllegalAccessException. "); return false; } catch (InvocationTargetException e) { Debug.logError("InvocationTargetException."); return false; } catch (NoSuchMethodException e) { Debug.logError("NoSuchMethodException."); return false; } } return true; } /** * 从原始数据库中读取原始数据。是根据HQL来构造导出语句,还是根据Entity工具的Criteria来构造导出语句,应该由用户来选择。 * @param projectIds 项目ID * @param select Select语句 * @return 原始数据的实体列表 */ protected List importDataTemplate(Integer[] projectIds){ String hql = createSelectHQL(projectIds); HqlReturnBuilder hqlRb = new HqlReturnBuilder(hql); try { return RecordContainer.doSelect(new Criteria(), hqlRb); } catch (QueryException e) { e.printStackTrace(); return null; } } /** * 构造从原始数据库中取数据的SELECT语句。 * @param projectIds * @return */ protected abstract String createSelectHQL(Integer[] projectIds); /** * 创建HSQL表。 * @return */ protected boolean createHSQLTable(){ PreparedStatement ps; try { ps = connection.prepareStatement(getDropTableSQL()); ps.executeUpdate(); ps.close(); ps = connection.prepareStatement(getSchema()); ps.executeUpdate(); ps.close(); return true; } catch (SQLException e) { e.printStackTrace(); return false; } } }
下面是子类的示例:代码很简单吧:)
- public class ProjectInfoExporter extends AbstractDataExporter {
- public ProjectInfoExporter(Connection connection) {
- super(connection,"ProjectInfo");
- }
- @Override
- protected String createSelectHQL(Integer[] projectIds) {
- String selectHQL = "from ProjectInfo project where project.projectId = ";
- StringBuffer sb = new StringBuffer(selectHQL).append(projectIds[0]);
- if(projectIds.length == 1){
- return sb.toString();
- }else{
- for (int i = 1; i < projectIds.length; i++) {
- sb.append("or project.projectId = ").append(projectIds[i]);
- }
- return sb.toString();
- }
- }
- }
public class ProjectInfoExporter extends AbstractDataExporter { public ProjectInfoExporter(Connection connection) { super(connection,"ProjectInfo"); } @Override protected String createSelectHQL(Integer[] projectIds) { String selectHQL = "from ProjectInfo project where project.projectId = "; StringBuffer sb = new StringBuffer(selectHQL).append(projectIds[0]); if(projectIds.length == 1){ return sb.toString(); }else{ for (int i = 1; i < projectIds.length; i++) { sb.append("or project.projectId = ").append(projectIds[i]); } return sb.toString(); } } }
SchemaGenerator:通过解析实体定义xml,以及数据类型定义xml来得到相关的Schema以及INSERT, DROP TABLE语句:)
- package ...
- import ...
- /**
- * JOB:自动生成HSQL DB相关的Schema,
- * @author wanzhigang
- *
- */
- public class SchemaGenerator {
- private Map entityMap;
- private Map fieldTypeDefMap;
- private final static String module = SchemaGenerator.class.getName();
- public static SchemaGenerator instance = new SchemaGenerator();
- private SchemaGenerator() {
- this.entityMap = initializeEntityMap();
- this.fieldTypeDefMap = initializeFieldTypeDefMap();
- }
- /**
- * 取得Entity Map。
- * @return
- */
- private Map initializeEntityMap(){
- return XMLModelEntityReader.getModelReader().getEntityCache();
- }
- /**
- * 取得FieldTypeDef Map.
- * @return
- */
- private Map initializeFieldTypeDefMap(){
- return XMLModelFieldTypeReader.getModelFieldTypeReader().getFieldTypeCache();
- }
- public Map getEntityMap() {
- return entityMap;
- }
- public Map getFieldTypeDefMap() {
- return fieldTypeDefMap;
- }
- /**
- * @param entityName 实体名称,实体定义文件中写好的实体名称。
- * @return 实体定义的HSQL Schema
- */
- public String generateHSQLSchema(String entityName){
- if(null==entityMap.get(entityName)){
- Debug.logError("no corresponding entity.");
- return null;
- }else{
- return getCreateTableSQLForHSQLDB((Entity)entityMap.get(entityName));
- }
- }
- /**
- * 生成Insert的SQL语句。
- * @param entityName
- * @return
- */
- public String generateInsertSQL(String entityName){
- if(null == entityMap.get(entityName)){
- Debug.logError("no corresponding entity.");
- return null;
- }else{
- return getCreateInsertSQL((Entity)entityMap.get(entityName));
- }
- }
- public String generateDropTableSQL(String entityName){
- Entity entity = (Entity)entityMap.get(entityName);
- if(null == entity){
- Debug.logError("no corresponding entity.");
- return null;
- }
- StringBuffer sb = new StringBuffer("DROP TABLE ");
- sb.append(getTableName(entity));
- sb.append(" IF EXISTS;");
- return sb.toString();
- }
- private String getCreateInsertSQL(Entity entity) {
- StringBuffer sb = new StringBuffer("INSERT INTO ");
- sb.append(getTableName(entity));
- sb.append(" VALUES (");
- for (int i = 0; i < entity.getField().length; i++) {
- sb.append(
发表评论
-
SQL注入攻击防御方案
2012-03-22 12:38 0http://www.iteye.com/topic/1121 ... -
oracle锁
2011-03-17 16:30 859ORACLE里锁有以下几种模式: 0:none ... -
oracle 查看字符集与修改字符集
2010-09-27 12:52 1057oracle字符集 Oracle字符集是一个字节数据的解释 ... -
PL SQL developer 导出数据
2010-09-26 19:55 1628如果只导出表的结构( ... -
主流数据库分页的SQL语句
2010-08-29 21:24 760主流数据库对分页查询都有不同程度的支持,下面分别是SQLSer ... -
oracle日期函数集锦
2010-08-25 11:21 727一、 常用日期数据格式 1.Y或YY或YYY 年的最后一位, ... -
Mysql监控(摘录)
2010-07-09 08:58 909无论是DBA或是SA,监控的目标都很明确,无外乎: 1.快速的 ... -
mysql日期函数全攻略
2009-12-25 10:49 795对于每个类型拥有的值范围以及并且指定日期何时间值的有效格式的描 ... -
mysql alter 语句用法,添加、修改、删除字段等
2009-11-29 13:51 966//主键549830479 alter table t ... -
Hsqldb简介和基本使用
2009-01-14 11:05 974Hsqldb是一个开放源代码的JAVA数据库,其具有标准的SQ ... -
两个小命令
2008-10-31 17:41 7331. sc delete 服务名称 sc的话就可以看到相 ... -
mysql开发工具套件
2008-10-31 17:33 1156mysql4.0不支持嵌套查询,需要转换到mysql5上来。m ... -
Hsqldb简介和基本使用
2008-10-31 16:57 1068Hsqldb是一个开放源代码的JAVA数据库,其具有标准的S ... -
SQL Server 技术公告-How to 解决死锁
2008-10-17 11:10 2038简介 <script type="t ...
相关推荐
mysql数据库导入导出命令mysql数据库导入导出命令
在不同操作系统或MySQL版本情况下,直接拷贝文件的方法可能会有不兼容的情况发生。所以一般推荐用SQL脚本形式导入。
NULL 博文链接:https://topbox163.iteye.com/blog/657178
MySQL数据库导出与导入的方法
自己用JAVA做的第一个swing程序,用来快速导入导出MySQL数据库,导入整个库6万条数据只需要5秒钟,处女作 见笑了。 使用方法:需要系统支持JAVA环境并安装了MySQL,直接运行DBdump.jar,填好相应项,按步骤导入导出...
linux下操作mysql数据库,导出数据库,只导出表结构,只导出数据。大汇总,挺全面的。希望对linux下用mysql数据库导数据的使用提供帮助
Linux系统下MySQL数据库的导入导出
进入mysql数据库控制台, 如mysql -u root -p mysql>use 数据库 然后使用source命令,后面参数为脚本文件(如这里用到的.sql) mysql>source wcnc_db.sql B:使用mysqldump命令 mysqldump -u username -p dbname C:...
mysql/mariadb无需创建数据库,直接导入数据库的脚本:无需手动创建数据库,直接新建数据库+导入表结构+插入记录
mysql 中Source的用法,以及mysqldump语句的命令
MySQL导出导入命令的用例 _数据库技巧
mysql数据库导入sql文件:Mysql导入导出.sql文件的方法
python源码:使用python在excel和mysql数据库进行导入导出
mysql数据库导入,导出方法,mysql,数据库,导出,导入
mysql数据库大数据量导入导出多种方法,该文档详述SELECT * INTO和mysqldump导出方法。导入方法中包括导入sql,txt文件和load data三种方法
NULL 博文链接:https://zhouwenjun.iteye.com/blog/526159
数据库的导入导出,MySQL Migration Toolkit:数据库迁移 MySQL Administrator:MySQL管理器 MySQL Query Browser:用于数据查询的图形化客户端 MySQL Workbench:DB Design工具
NULL 博文链接:https://duanfei.iteye.com/blog/801767
java实现mysql数据库的表导出到excel文件,已经基本的demo包含jar包,可以导入的eclipse里面。比较好用吧