JDBC访问embedded Derby数据库

[b]Apache Derby:[/b] [url]http://db.apache.org/derby/index.html[/url]

最近在一个业余项目中使用了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 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()断开与数据库的连接。

你可能感兴趣的:(Java)