如何使用 JDBC 程式連線到 Derby 資料庫?


Apache Derby 是一個關係資料庫管理系統,完全基於(用/實現於)Java 程式語言。它是 Apache 軟體基金會開發的一個開源資料庫。

安裝 Derby

按照以下步驟安裝 Derby:

  • 訪問 Apache Derby 首頁 **https://db.apache.org/derby/**。點選“下載”選項卡。

  • 選擇並點選 Apache Derby 最新版本的連結。

  • 點選所選連結後,您將被重定向到 apache derby 的 **發行版** 頁面。在這裡您可以看到,Derby 提供的發行版包括 db-derby-bin、db-derbylib.zip、db-derby-lib-debug.zip 和 db-derby-src.zip。

  • 下載 **db-derby-bin** 資料夾。將其內容複製到您想要安裝 Apache Derby 的單獨資料夾中(例如,**C:\Derby**)。

現在,要使用 Derby,

  • 確保您已透過傳遞 Java 安裝資料夾的 bin 資料夾位置設定了 **JAVA_HOME** 變數,並將 **JAVA_HOME/bin** 包含在 PATH 變數中。
  • 建立一個新的環境變數 **DERBY_HOME**,其值為 C:\Derby。
  • db-derby-bin 發行版的 bin 資料夾(我們將其更改為 C:\Derby\bin)包含所有必需的 jar 檔案。

示例

下面的 JDBC 程式建立與 Apache Derby 資料庫的連線,建立一個名為 employeedata 的表,向其中插入記錄,檢索並顯示錶的內容。

public class InsertData {
   public static void main(String args[]) throws Exception {
      //Registering the driver
      Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
      //Getting the Connection object
      String URL = "jdbc:derby:mydatabs;create=true";
      Connection conn = DriverManager.getConnection(URL);
      //Creating the Statement object
      Statement stmt = conn.createStatement();
      //Creating a table in Derby database
      String query = "CREATE TABLE EmployeeData( "
         + "Id INT NOT NULL GENERATED ALWAYS AS IDENTITY, "
         + "Name VARCHAR(255), "
         + "Salary INT NOT NULL, "
         + "Location VARCHAR(255), "
         + "PRIMARY KEY (Id))";
      stmt.execute(query);
      System.out.println("Table created");
      //Inserting data
      query = "INSERT INTO EmployeeData("
         + "Name, Salary, Location) VALUES "
         + "('Amit', 30000, 'Hyderabad'), "
         + "('Kalyan', 40000, 'Vishakhapatnam'), "
         + "('Renuka', 50000, 'Delhi'), "
         + "('Archana', 15000, 'Mumbai'), "
         + "('Trupthi', 45000, 'Kochin'), "
         + "('Suchatra', 33000, 'Pune'), "
         + "('Rahul', 39000, 'Lucknow'), "
         + "('Trupthi', 45000, 'Kochin')";
      stmt.execute(query);
      System.out.println("Values inserted");
      //Retrieving data
      ResultSet rs = stmt.executeQuery("Select * from EmployeeData");
      System.out.println("Contents of the table EmployeeData table:");
      while(rs.next()) {
         System.out.print("ID: "+rs.getInt("ID")+", ");
         System.out.print("Name: "+rs.getString("Name")+", ");
         System.out.print("Salary: "+rs.getInt("Salary")+", ");
         System.out.print("Location: "+rs.getString("Location"));
         System.out.println();
      }
   }
}

輸出

Table created
Values inserted
Contents of the table EmployeeData table:
ID: 1, Name: Amit, Salary: 30000, Location: Hyderabad
ID: 2, Name: Kalyan, Salary: 40000, Location: Vishakhapatnam
ID: 3, Name: Renuka, Salary: 50000, Location: Delhi
ID: 4, Name: Archana, Salary: 15000, Location: Mumbai
ID: 5, Name: Trupthi, Salary: 45000, Location: Kochin
ID: 6, Name: Suchatra, Salary: 33000, Location: Pune
ID: 7, Name: Rahul, Salary: 39000, Location: Lucknow
ID: 8, Name: Trupthi, Salary: 45000, Location: Kochin

更新於:2019年7月30日

2K+ 次瀏覽

啟動您的 職業生涯

完成課程獲得認證

開始學習
廣告