最近在一個業餘項目中使用了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()斷開與數據庫的連接。