一. Derby能提供什麼?html
Derby是一個基於Java和SQL ,開源的 RDBMS 。徹底使用Java實現,他爲用戶提供了一個小巧的基於標準的數據庫引擎,他能夠嵌入到任Java解決方案中,他確保數據完成並提供複雜的事務支持。Derby的數據徹底存儲在磁盤上,他是一可攜帶的數據庫,你能夠將他從一臺機器拷貝到另外一臺機器。更多信息請訪問 http://db.apache.org/derby
java
二. Derby如何開始?sql
Derby的兩種模式:內嵌模式【他和你應用程序在同一個JVM中,不須要單獨配置和你的應用程序有相同的生命週期】、服務模式【運行在獨立的JVM中,須要網絡配置這點相似於Mysql等數據庫】數據庫
系統要求:Derby 使用 Java 開發依賴於JDK ,要求JDK版本 1.5+ 。查看版本能夠使用命令 java -versionapache
下載安裝Derby : 下載地址: http://db.apache.org/derby/derby_downloads.html 網絡
設置環境變量: DERBY_HOME 變量值=解壓的目錄,目錄的結構就不解釋了。app
設置Path 和 CLASSPATH : 在 Path中增長 %DERBY_HOME%\bin 、在 CLASSPATH 中增長 %DERBY_HOME%\lib 工具
三. 有哪些工具?怎樣用?spa
ij 是Derby交互式JDBC腳本工具,使用該工具能夠使用SQL腳本和數據庫交互。命令行
配置完環境變量後有不少種方式啓動ij工具:
1.在命令行使用 java -jar %DERBY_HOME%\lib\derbyrun.jar ij
2.在命令行使用 java org.apache.derby.tools.ij
3.直接使用 ij 或 雙擊%DERBY_HOME%\bin 下 ij.bat
啓動後看到 說明啓動成功
鏈接到數據庫 connect 'jdbc:derby:mydb;create=true';
說明:connect 是ij工具內置的命令用於鏈接到數據庫,mydb是數據庫名稱,create=true 說明若是數據庫不存在就建立,建立位置爲當前工做空間,該位置能夠指定;
新建表 create table mytable(id int primary key,name varchar(20) ) ;
錄入數據 insert into mytable(10,'shi'),(20,'ershi'),(30,'sanshi');
查詢數據 select * from mytable;
如今咱們已經有一個名爲mydb的derby數據庫了。而且數據庫中已有一個表mytable和三條數據。
sysinfo 查詢Derby版本信息和環境信息
dblook
SignatureChecker
PlanExporter
四.JDBC用法(代碼)
數據庫鏈接類 DBConnection
public class DBConnection { private final static String driver = "org.apache.derby.jdbc.EmbeddedDriver"; public static Connection getConnection(){ String dbName="mydb"; Connection conn=null; try { Properties props = new Properties(); // connection properties // providing a user name and password is optional in the embedded // and derbyclient frameworks //props.put("user", "user1"); //props.put("password", "user1"); Class.forName(driver).newInstance(); System.out.println("Loaded the appropriate driver"); conn = DriverManager.getConnection("jdbc:derby:"+dbName+";create=true", props); System.out.println("Connected to and created database " + dbName); } catch (Exception ex) { ex.printStackTrace(); } return conn; } public static void shutdwon(){ try { DriverManager.getConnection("jdbc:derby:;shutdown=true"); } catch (SQLException se) { if (( (se.getErrorCode() == 50000) && ("XJ015".equals(se.getSQLState()) ))) { // we got the expected exception System.out.println("Derby shut down normally"); // Note that for single database shutdown, the expected // SQL state is "08006", and the error code is 45000. } else { // if the error code or SQLState is different, we have // an unexpected exception (shutdown failed) System.err.println("Derby did not shut down normally"); se.printStackTrace(); } } } }
數據庫訪問類 myTableDao
public class myTableDao{ public int save(String email){ Connection conn = DBConnection.getConnection(); int result=0; try { conn.setAutoCommit(false); PreparedStatement pstm = conn.prepareStatement("insert into location values (?)"); pstm.setString(1, email); result=pstm.executeUpdate(); conn.commit(); if(pstm != null){ pstm.close(); pstm = null; } if (conn != null) { conn.close();conn = null; } } catch (SQLException ex) { ex.printStackTrace(); } return result; } public int delete(String email){ Connection conn = DBConnection.getConnection(); int result=0; try { conn.setAutoCommit(false); PreparedStatement pstm = conn.prepareStatement("delete from location where addr=?"); pstm.setString(1, email); result=pstm.executeUpdate(); conn.commit(); if(pstm != null){ pstm.close(); pstm = null; } if (conn != null) { conn.close();conn = null; } } catch (SQLException ex) { ex.printStackTrace(); } return result; } public int update(String email,String nemail){ Connection conn = DBConnection.getConnection(); int result=0; try { conn.setAutoCommit(false); PreparedStatement pstm = conn.prepareStatement("update location set addr=? where addr=?"); pstm.setString(1, nemail); pstm.setString(2, email); result=pstm.executeUpdate(); conn.commit(); if(pstm != null){ pstm.close(); pstm = null; } if (conn != null) { conn.close();conn = null; } } catch (SQLException ex) { ex.printStackTrace(); } return result; } public List<String> query(){ Connection conn = DBConnection.getConnection(); List<String> list=null; try { PreparedStatement pstm = conn.prepareStatement("SELECT addr FROM location"); ResultSet rs = pstm.executeQuery(); list = new ArrayList<String>(); while (rs.next()) { list.add(rs.getString(1)); } if(rs != null){ rs.close(); rs = null; } if(pstm != null){ pstm.close(); pstm = null; } if (conn != null) { conn.close();conn = null; } } catch (SQLException ex) { ex.printStackTrace(); } return list; } //初始化表 public void initTable(){ try { Connection conn = DBConnection.getConnection(); conn.setAutoCommit(false); Statement s = conn.createStatement(); s.execute("create table location(addr varchar(40))"); System.out.println("Created table location"); conn.commit(); if(s != null){ s.close(); s= null; } if (conn != null) { conn.close();conn = null; } } catch (SQLException ex) { ex.printStackTrace(); } } }
補充:須要在工程中導入 derby.jar
五.分頁語句如何寫
SELECT id, jobid, logtime, msgtext FROM log_table where 1=1 order by logtime desc OFFSET ? ROWS FETCH NEXT ? ROWS ONLY
說明:本身試試