2014-10-11 42 views

回答

2

德比最基本的安裝到您的應用程序是相當輕鬆的。這實際上只是將庫/ jar放在類路徑上。 Netbeans已經有了這個庫,所以你不必下載它。如果由於任何原因,它不會,您可以從here下載它。解壓縮之後,只需將derby.jar(以及其他必要的話)添加到類路徑中即可。

基本上來自Netbeans,從您的項目中,右鍵單擊[庫]並選擇[添加庫]。

enter image description here

然後,只需選擇[Java DB的]庫

enter image description here

如果你下載的庫,然後代替[添加庫],選擇[添加瓶]和搜索jar下載到的地方。

這些附帶的Netbeans庫

enter image description here

然後你就可以在你的應用程序使用的數據庫的罐子。嵌入式版本與您的應用程序在相同的JVM上運行,因此您可能需要自己照顧數據庫的啓動和關閉。這裏有一個示例應用程序,它啓動-createdb-inserts-selecting-shutsdown。

import java.sql.*; 

public class DerbyProject { 
    public static void main(String[] args) throws Exception { 
     /* ------- Start DB ----------- */ 
     final String driver = "org.apache.derby.jdbc.EmbeddedDriver"; 
     Class.forName(driver).newInstance(); 

     final String protocol = "jdbc:derby:"; 
     final String dbName = "derbyDB"; 
     Connection connection = DriverManager.getConnection(
       protocol + dbName + ";create=true"); 
     System.out.println("===== Started/Connected DB ====="); 

     /* 
     * Drop table for testing. If we don't drop, running the 
     * same program will fail, if we start our application over 
     * as the new table has been persisted 
     */ 
     final String dropSql = "drop table users"; 
     Statement statement = connection.createStatement(); 
     try { 
      statement.execute(dropSql); 
      System.out.println("===== Dropped Table 'users' ====="); 
     } catch (SQLException e) { 
      if (!e.getSQLState().equals("42Y55")) { 
       throw e; 
      } 
     } 

     /* ----- Creeate 'users' table ----- */ 
     final String createSql = "create table users (id int, name varchar(32))"; 
     statement.execute(createSql); 
     System.out.println("===== Created Table 'users' ====="); 

     /* ----- Insert 'peeskillet' into 'users' ----*/ 
     final String insertSql = "insert into users values (1 , 'peeskillet')"; 
     statement.execute(insertSql); 
     System.out.println("===== inserted 'peeskillet into 'users' ====="); 

     /* ----- Select from 'users' table ----- */ 
     final String selectSql = "select name from users where id = 1"; 
     ResultSet rs = statement.executeQuery(selectSql); 
     if (rs.next()) { 
      System.out.println("===== Selected from 'users' with id 1 \n" 
        + "\t\t\t result: " + rs.getString("name") + " ====="); 
     } 

     /* ------ Shut Down DB ------- */ 
     try { 
      DriverManager.getConnection("jdbc:derby:;shutdown=true"); 
     } catch (SQLException se) { 
      if (((se.getErrorCode() == 50000) 
        && ("XJ015".equals(se.getSQLState())))) { 
       System.out.println("Derby shut down normally"); 
      } else { 
       System.err.println("Derby did not shut down normally"); 
       throw se; 
      } 
     } 

     statement.close(); 
     rs.close(); 
     connection.close(); 
    } 
} 

當我們建造它時,NetBeans默認的構建應該把罐子到dist\lib,把那些罐子上的MANIFEST.MF classpath中。你可以在命令行中運行jar來測試它

enter image description here

如果你打開文件,查看在NetBeans中,你可以看到實際被存儲的數據是在哪裏。

enter image description here


有關德比和德比教程的詳細信息:

相關問題