Topic: (从一个数据库读一个表导入另外一个数据库)

  Print this page

1.(从一个数据库读一个表导入另外一个数据库) Copy to clipboard
Posted by: zhandidoushi
Posted on: 2005-05-31 11:21

请尽量用准确的文字描述作为标题

Subject: 菜菜问题,帮帮忙哦

写个小程序。
功能就是从一个数据库中读一个表的数据出来
然后导入另外一个数据库的相同的一个表中
应该怎么搞啊
总不会是先把数据都读到一个vector里面,然后再把vector中的数据insert到新表把

2.Re:(从一个数据库读一个表导入另外一个数据库) [Re: zhandidoushi] Copy to clipboard
Posted by: why
Posted on: 2005-05-31 20:12

可以不用 vector
可以是一面读,一面insert (batch insert)
可能要處理encoding -- oh, it's mentioned downstairs

3.Re:(从一个数据库读一个表导入另外一个数据库) [Re: zhandidoushi] Copy to clipboard
Posted by: jameszhang
Posted on: 2005-05-31 21:05

要看你是用的是什么数据库,你走运都不用编码,哈哈哈哈哈

4.Re:(从一个数据库读一个表导入另外一个数据库) [Re: zhandidoushi] Copy to clipboard
Posted by: undefined
Posted on: 2005-06-02 22:13

连接数据库,读取数据,然后或者直接一边读一边插,或者先做保存,建议先做保存,至于保存的形式就太多了,简单的,数组,集合都可以,如果业务上有需要,还有xml,甚至excel.

5.Re:(从一个数据库读一个表导入另外一个数据库) [Re: undefined] Copy to clipboard
Posted by: YuLimin
Posted on: 2005-06-04 09:17

如果表与数据一样的话,建议用Ant + DbUnit,比较方便。。。

参考:我的Blog文章
使用DbUnit,可以用Ant的任务来实现,也可以直接写DbUnit的测试代码实现
http://iamin.blogdriver.com/iamin/632909.html
Ant: http://ant.apache.org/

Apache Ant is a Java-based build tool. In theory, it is kind of like Make, but without Make's wrinkles.

DbUnit:http://www.dbunit.org/

DbUnit is a JUnit extension (also usable with Ant) targeted for database-driven projects that, among other things, puts your database into a known state between test runs. This is an excellent way to avoid the myriad of problems that can occur when one test case corrupts the database and causes subsequent tests to fail or exacerbate the damage.

DbUnit has the ability to export and import your database data to and from XML datasets. Since version 2.0, DbUnit can works with very large dataset when use in streaming mode. DbUnit can also helps you to verify that your database data match expected set of values.

JUnitDoclet:http://www.junitdoclet.org/

Why wasn't this tested before? This question often arises shortly before a deadline. Our answer: Because testing was not easy enough.

Especially creating and maintaining a test structure is often used as an excuse. JUnitDoclet lowers the step toward JUnit. It generates skeletons of TestCases based on your application source code. And it supports you to reorganize tests during refactoring. Suddenly all the excuses don't count any longer.

MySQL数据库:Pagination,表:Persioninfo信息如下:

# Host: localhost
# Database: pagination
# Table: 'personinfo'
#
CREATE TABLE `personinfo` (
`id` varchar(100) NOT NULL default '',
`name` varchar(50) NOT NULL default '',
`sex` tinyint(1) NOT NULL default '0',
`mobile` varchar(50) NOT NULL default '0',
`address` varchar(50) NOT NULL default '',
`memo` varchar(50) default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312;

一、用Ant的任务来实现

  1、DataSelt.xml内容如下:

<?xml version="1.0" encoding="GBK"?>
<dataset>
<Personinfo id="9001" name="YuLimin" sex="0" mobile="13800138000" address="中国福建莆田" memo="Ant DBUnit Test"/>
<Personinfo id="9002" name="俞Limin" sex="1" mobile="13800138000" address="中华人民共和国" memo="Ant DBUnit Test"/>
<Personinfo id="9003" name="俞黎敏" sex="0" mobile="13800138000" address="中华人民共和国福建" memo="Ant DBUnit Test"/>
</dataset>

  2、Ant 的 build.xml内容如下,请进行相应的改动:

<?xml version="1.0" encoding="GBK"?>
<project name="DbUnitTest" basedir="." default="initdb">
<!-- 定义DbUnit需要的包,包括DbUnit包和jdbc包。 -->
<property name="lib.dir" value="E:\OpenSource"/>
<property name="dbunit.jar" value="${lib.dir}\DBUnit\dbunit-2.1.jar"/>
<property name="jdbc.jar" value="${lib.dir}\MySQL\lib\mysql-connector-java-3.1.7-bin.jar"/>
<property name="junit.jar" value="${lib.dir}\JUnit\junit.jar"/>
<property name="junitdoclet.jar" value="${lib.dir}\JUnitDoclet\JUnitDoclet.jar"/>
<property name="classes.dir" value="./classes"/>
<!-- 在全局属性定义中定义数据库连接的url,driver,userid,password,进行多个操作可以达到重用 -->
<property name="dburl" value="jdbc:mysql://localhost/Pagination"/>
<property name="dbdriver" value="com.mysql.jdbc.Driver"/>
<property name="dbuserid" value="root"/>
<property name="dbpwd" value="password"/>
<path id="DbUnit.classpath">
<pathelement location="${dbunit.jar}"/>
<pathelement location="${jdbc.jar}"/>
<pathelement location="${junit.jar}"/>
<pathelement location="${junitdoclet.jar}"/>
</path>
<target name="init">
<!-- 定义DbUnit的Ant任务类 -->
<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask" classpathref="DbUnit.classpath"/>
</target>
<target name="initdb" depends="init">
<dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
<!-- operation的类型有:UPDATE 、INSERT、DELETE 、DELETE_ALL 、TRUNCATE 、REFRESH(MSSQL_REFRESH)、CLEAN_INSERT(MSSQL_CLEAN_INSERT)、NONE -->
<!-- (注意:MSSQLServer中在CLEAN_INSERT和REFRESH中要使用后面的MSSQL_REFRESH和MSSQL_CLEAN_INSERT) -->
<!-- INSERNT仅插入数据 -->
<!-- CLEAN_INSERT清除并插入数据 -->
<operation type="INSERT" src="DataSet.xml"/>
</dbunit>
</target>
<target name="export" depends="init">
<!-- 导出全部包含有数据的表的数据出来 -->
<dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
<export dest="Export.xml"/>
</dbunit>
</target>
<target name="select" depends="init">
<!-- 导出一个由查询语句产生的数据集和指定的一个表的数据 -->
<dbunit driver="${dbdriver}" supportBatchStatement="false" url="${dburl}" userid="${dbuserid}" password="${dbpwd}" classpathref="DbUnit.classpath">
<export dest="Select.xml">
<query name="Personinfo" sql="Select * From Personinfo Where name='YuLimin'"/>
<table name="Personinfo"/>
</export>
</dbunit>
</target>
</project>

3、运行:ant或ant initdb或ant export或ant select不同的任务

二、直接写DbUnit的测试代码实现,DbUnitTest.java的代码如下:

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;

import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
import org.dbunit.operation.DatabaseOperation;

/**
*

Title: 运行DbUnit测试将数据导入数据库。

*
*
Description: 运行DbUnit测试将数据导入数据库。

*
*
Copyright: Copyright (c) 2004

*
*
Company: Beyond DayBreak Office

* @author YuLimin
* @version 1.0
*/
public class DbUnitTest extends DatabaseTestCase
{
public DbUnitTest(String name)
{
super(name);
}

/**
*
* @see org.dbunit.DatabaseTestCase#getConnection()
* @throws Exception
* @return IDatabaseConnection
*/
protected IDatabaseConnection getConnection() throws Exception
{
Class driverClass = Class.forName("com.mysql.jdbc.Driver");
Connection jdbcConnection = DriverManager.getConnection("jdbc:mysql://localhost/Pagination","root","password");
return new DatabaseConnection(jdbcConnection);
}

/**
*
* @see org.dbunit.DatabaseTestCase#getDataSet()
* @throws Exception
* @return IDataSet
*/
protected IDataSet getDataSet() throws Exception
{
return new FlatXmlDataSet(new FileInputStream("DataSet.xml"));
}

/**
* getSetUpOperation
*
* @return DatabaseOperation
* @throws Exception
*/
protected DatabaseOperation getSetUpOperation() throws Exception
{
return DatabaseOperation.REFRESH;
}

/**
* getTearDownOperation
*
* @return DatabaseOperation
* @throws Exception
*/
protected DatabaseOperation getTearDownOperation() throws Exception
{
return DatabaseOperation.NONE;
}

/**
* 运行DbUnit测试将数据导入数据库。
*/
public void testDbUnit()
{
System.out.println("DbUnit Testing...");
}
}


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923