使用JasperReport显示BerkeleyDB数据库的内容
JasperReport是一个流行的Java开源报表工具,BerkeleyDB是一个流行的开源嵌入式数据库产品. 长久以来,JasperReport无法显示BerkeleyDB中的数据. 但是,现在这一状况得到了改善.JasperForge在2008-08-05正式发布了BerkeleyDB的Driver,这意味着,以后可以使用 JasperReport来显示存储于BerkeleyDB中的内容了.
本文将主要介绍如何利用JasperReport来显示 BerkeleyDB的数据.BerkeleyDB产品家族包含了C 语言 Edition(Core), Java Edition,和XML Edition. 本文针对的是Core Edition最新的Release, 4.7.25. 为了给读者一个直观的印象.本文将创建一个使用BerkeleyDB用于存储学生信息的数据库,而后使用JasperReport显示其内容.
1 下载并安装/编译BerkeleyDB,下载地址为
http://www.oracle.com/technology/software/products/berkeley-db/db/index.html
可以选择下载.msi或者压缩文件,如果下载的是压缩文件,需要自己来编译.BerkeleyDB提供了针对Linux的configure/make和针对windows的.sln和.dsw文件,编译方法不在此累述.
需要注意的是,本处需要java的支持,故而,需要编译java的库. 对于configure/make而言,在configure的时候需要–enable-java,用.sln/.dsw的则需要编译Project db_java.
2 创建Student数据库,本处只给出C代码的片段.
static char *env_home=”D:/tmp/jasper-env”;
/* This is the struct for student */
typedef struct student {
char student_no[20]; /* student no. */
char student_name[20]; /* student name */
char sexuality[8]; /* sex */
int sex_code; /* code for sex male as one, female as two, and 0 stands for unknown.*/
char class_no[20]; /* class no. */
char class_name[100]; /* class name */
char brief_class_name[60]; /* brief class name */
char major_name[100]; /* major name */
char register_daymonth[20]; /* the year and month for entering */
char nationality[50]; /* nationality */
char birthday[20]; /* the year and month for birthday */
char id_no[30]; /* ID card no. */
}STUDENT;
static char *student_data_infile=”D:/tmp/stu/student.data.out”;
static char *student_db_name=”student.db”;
int create_db_of_student(int argc, char *argv[]){
DB_ENV *dbep;
DB *dbp;
STUDENT stu;
DBT dbt_key,dbt_data;
char buff[500];
int len=0;
FILE *srcfp=NULL;
char *lineptr=NULL;
int intval=0;
int ret=0;
u_int32_t envoflag=DB_CREATE|DB_RECOVER|DB_INIT_TXN|DB_INIT_MPOOL|DB_INIT_LOCK|DB_INIT_LOG;
ret=db_env_create(&dbep,0);
ret=dbep->remove(dbep,env_home,DB_FORCE);
ret=db_env_create(&dbep,0);
ret=dbep->open(dbep,env_home,envoflag,0);
ret=db_create(&dbp,dbep,0);
ret=dbp->remove(dbp,student_db_name,NULL,0);
ret=db_create(&dbp,dbep,0);
ret=dbp->open(dbp,NULL,student_db_name,NULL,DB_BTREE,DB_CREATE,0);
srcfp=fopen(student_data_infile,”r”);
while(1){
/*field student_no*/
lineptr=fgets(buff,500,srcfp);
if(lineptr==NULL)
break ;
if(buff[strlen(lineptr)-1]==’n')
buff[strlen(lineptr)-1]=’