DBCrawler是一个纯java编写的轻量级面向数据库结构的爬虫工具.
最近,DBCrawler发布了其1.4版本,主要支持的数据库包括Apache Derby,H2 Database,HsqlDB,IBM DB2,MysqL,Postgresql,同时支持试图操作.其最大的特点就是可以无视数据库类型,轻易获取数据库表结构并以POJO方式返回给用户,使用实例:
1.加入maven库:
在你工程的pom文件中project下一级别加入库:
<repositories>
<repository>
<id>dbcrawler-repo</id>
<name>dbcrawler Maven2 Repository</name>
<url>http://dbcrawler.googlecode.com/svn/repo</url>
</repository>
</repositories>
然后加入依赖:
<!--dbcrawler-->
<dependency>
<groupId>com.avdheshyadav</groupId>
<artifactId>dbcrawler</artifactId>
<version>1.4</version>
</dependency>
2.使用代码
package com.taobao.zhaopin.dal.dao;
import java.io.IOException;
import java.sql.Connection;
import java.util.Set;
import org.springframework.orm.ibatis.sqlMapClientTemplate;
import org.unitils.UnitilsJUnit3;
import org.unitils.spring.annotation.SpringApplicationContext;
import org.unitils.spring.annotation.SpringBean;
import com.avdheshyadav.dbcrawler.ConfigEnum;
import com.avdheshyadav.dbcrawler.DBCrawler;
import com.avdheshyadav.dbcrawler.DbCrawlerException;
import com.avdheshyadav.dbcrawler.dbmodel.ColumnSet;
import com.avdheshyadav.dbcrawler.dbmodel.DataBase;
import com.avdheshyadav.dbcrawler.dbmodel.ForeignKey;
import com.avdheshyadav.dbcrawler.dbmodel.PrimaryKey;
import com.avdheshyadav.dbcrawler.dbmodel.Schema;
import com.avdheshyadav.dbcrawler.dbmodel.SchemaSet;
import com.avdheshyadav.dbcrawler.dbmodel.Table;
import com.avdheshyadav.dbcrawler.dbmodel.TableSet;
@SpringApplicationContext({"classpath:persistence.xml"})
public class DBCrawlerTest extends UnitilsJUnit3{
@SpringBean("sqlMapClientTemplate")
private sqlMapClientTemplate sqlMapClientTemplate;
public void testCrawler() throws DbCrawlerException,sqlException{
Connection connection = sqlMapClientTemplate.getsqlMapClient().getDataSource().getConnection();
DBCrawler dbCrawler = new DBCrawler(connection,ConfigEnum.MAXIMUM);
DataBase dataBase = dbCrawler.getDatabase();
System.out.println("productName :" + dataBase.getProductName() + " version:" + dataBase.getProductVersion());
SchemaSet schemaSet = dataBase.getSchemaSet();
Set<Schema> schemas = schemaSet.getSchemas();
for(Schema schema : schemas)
{
System.out.println("SchemaName :" + schema.getSchamaName());
TableSet tableSet = schema.getTableSet();
Set<Table> tables = tableSet.getTables();
//Iterate to fetch the tables
for(Table table : tables)
{
System.out.println("tableName :" + table.getTableName());
PrimaryKey primaryKey = table.getPrimaryKey();
System.out.println("pk_Name:"+primaryKey.getPkName() + " PrimaryKey Columns:" + primaryKey.getColumns());
ColumnSet columnSet = table.getColumnSet();
System.out.println("Table Columns:"+ columnSet.getColumns());
Set<ForeignKey> foreignKeys = table.getForeignKeys();
System.out.println("foreignKeys:"+foreignKeys);
}
}
}
}