Apache Derby: http://db.apache.org/derby/index.html
最近在一个业余项目中使用了derby数据库来存储task的创建及完成记录。derby是apache的一个开源数据库产品,有丰富的特性。它支持client/server模式外,也支持embedded模式,即只需一个包含embedded driver的jar包,就可以在代码内启动及关闭数据库。在小项目中使用嵌入式的数据库也是一个不错的选择。
这里使用jdbc来连接derby进行操作并无特别之处。只需要将embedded driver包derby.jar包含进class_path,将创建driver实例,使用通常jdbc代码即可访问。
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
Class.forName(driver).newInstance();
以下是用junit4框架写的explore代码:
package com.durian.derby;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Enumeration;
import java.util.Properties;
import org.junit.Test;
public class ExploreDerbyTest {
@Test
public void testCreateDB()
throws InstantiationException, IllegalAccessException, ClassNotFoundException, SQLException {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String dbName = "derbyDB";
String protocol = "jdbc:derby:";
Properties props = new Properties();
props.put("user", "user1");
props.put("password", "password");
Class.forName(driver).newInstance();
System.out.println(DriverManager.getLoginTimeout());
Enumeration<Driver> ds = DriverManager.getDrivers();
while (ds.hasMoreElements()) {
System.out.println(ds.nextElement());
}
Connection conn = DriverManager.getConnection(protocol+dbName+";create=true");
//Properties p = conn.getClientInfo();
//System.out.println(p);
conn.setAutoCommit(false);
Statement s = conn.createStatement();
//s.execute("create table location2(num int, addr varchar(40))");
//System.out.println(s.getFetchDirection() + " | " +
// s.getFetchSize() + " | " + s.getMaxFieldSize() + " | " +
// s.getMaxRows() + " | " + s.getQueryTimeout());
conn.commit();
conn.close();
}
@Test
public void testCreateTables()
throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
String driver = "org.apache.derby.jdbc.EmbeddedDriver";
String protocol = "jdbc:derby:";
String dbName="derbyDB";
String[] sqls = {
"create table mail_server(id int, name varchar(50), host varchar(50), port int, protocol varchar(10), is_deleted char(1), description varchar(100) )",
"create table job_history(id int, mail_server_id int, user_id varchar(50), password varchar(50), folder_name varchar(50), befor_days int, status varchar(10), " +
"created_time time, started_time time, stopped_time time)"
};
Class.forName(driver).newInstance();
Connection conn = DriverManager.getConnection(protocol+dbName);
conn.setAutoCommit(false);
Statement stmt = conn.createStatement();
for (int i = 0; i < sqls.length; i++) {
stmt.addBatch(sqls[i]);
}
int[] r = stmt.executeBatch();
System.out.println(r);
conn.commit();
conn.close();
}
}
执行testCreateDB()后将在当前路径下创建一个名为derbyDB文件夹,包含derby数据库的相库文件。关于derby的配置问题,还有待研究。
由于对jdbc不是很熟悉,顺便记一下jdbc访问数据库的过程,尽管以上代码可以演示说明:
(1)所有相关类及接口都在包java.sql中。另外还有javax.sql,都是扩展内容
(2)由DriverManager获得到要连接数据库的Connection
(3)Connection创建Statement或PreparedStatement
(4)Statement或PreparedStatement执行sql语句,有可能返回ResultSet结果集。可以对ResultSet进行遍历访问
(5)若有insert/update/delete等数据操作,需调用Connection的commit().(如果设置为不自动提交)
(6)conn.close()断开与数据库的连接。
分享到:
相关推荐
derby embedded模式默认是不需要用户名和密码的,但是用户验证这一需求是多么正常啊,怎么让derby embedded模式支持用户验证呢?本教程将带领你一步步揭开迷雾。Joy原创!
C#使用Embedded Firebird数据库示例 Firebird 数据库 内嵌数据库 .net data provider for firebird
在介绍SQL Server Mobile的基本概念和开发运行环境的基础上,详绅论述安装和配置SQL Server CE Mobile开发环境的步骤和方法,幵通过对一个数据库应用程序(AddressBook)开发过程的详绅讲解使读者完整理解和掌握SQL ...
Chapter 1 Software Engineering of Embedded and Real-Time Systems Chapter 2 Embedded Systems Hardware/Software Co-Development Chapter 3 Software Modeling for Embedded Systems Chapter 4 Software Design ...
数据库原理3.04 Embedded SQL
示例-java-spring-genericdao 这是一个使用 Spring 4.1 MVC、Core、... 如果您想在 Tomcat 中运行 Apache Embedded Derby 数据库(默认情况下是这个数据库),如果数据库驱动程序是 10.11 或更新版本,请检查 \
URLName = "jdbc:firebirdsql:embedded:E:/Project/test.fdb"; user = "sysdba"; password = "masterkey"; 今日:研究FB数据库许久,参考了网友的资料,不胜感激!于MyEclipse6.0.1上调试成功,在此同大家分享。
Windows Xp Embedded 安裝流程
WINDOWS EMBEDDED COMPACT 7(WINCE7)的安装文件和KEY
h2数据库引擎开发文档h2.pdf Welcome to H2, the Java SQL database. The main features of H2 are: Very fast, open source, JDBC API Embedded and server modes; in-memory databases Browser based Console...
Title: Embedded Systems: Introduction to Armxae Cortexu2122-M Microcontrollers, 5th Edition Author: Jonathan Valvano Length: 506 pages Edition: Fifth Language: English Publisher: Jonathan Valvano ...
数据库算法源代码,一本关于uCOS-II的经典教程,特别适合嵌入式系统开发的人员
使用插件Embedded Browser 实现unity与网页交互, unity内嵌浏览器Embedded Browser V2.1.0(适用于PC端) 所支持的Unity版本:5.6.3 及以上版本
数据库原理及应用 Database Principle and Application 第八章 数据库编程 第八章 数据库编程 8.1 嵌入式SQL 8.2 存储过程 8.3 ODBC编程 8.4 小结 8.1 嵌入式SQL 这两种方式细节上有差别,在程序设计的环境下, SQL...
what the benefits of embedded Linux are over other RTOSs. It discusses in detail the features of various open source and commercial embedded Linux distributions available. The chapter concludes by ...
Mastering Embedded Linux Programming.pdf Mastering Embedded Linux Programming.pdf
Such stuff is termed embedded systems, and this book shows how to design and develop embedded systems at a professional level. Because yes, many people quietly make a successful career doing just ...
使用HSQLDB,H2和Derby的Spring 4 MVC +嵌入式数据库示例的模板。 ### 1。 使用的技术 Maven的3.0 春天4.1.6.RELEASE HSQLDB 2.3.2 氢气1.4.187 德比10.11.1.1 ### 2。 在本地运行该项目 $ git clone ...
unity 内嵌浏览器最新版本Embedded Browser 3.1.0
Considered a standard industry resource, the Embedded Systems Handbook provided researchers and technicians with the authoritative information needed to launch a wealth of diverse applications, ...